> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bunny.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Go API

> Deploy a Go API to Magic Containers

This guide walks you through building and deploying a simple Go API to Magic Containers using GitHub Container Registry. You'll need:

* A GitHub account for source code and container registry

<Tip>
  Don't have a bunny.net account yet? [Sign up](https://dash.bunny.net/auth/register) and enable Magic Containers to get started.
</Tip>

## Create the Go API

Create a new directory with the following files:

<CodeGroup>
  ```go main.go theme={null}
  package main

  import (
  	"encoding/json"
  	"log"
  	"net/http"
  	"os"
  )

  type Response struct {
  	Message string `json:"message"`
  }

  func main() {
  	port := os.Getenv("PORT")
  	if port == "" {
  		port = "80"
  	}

  	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  		w.Header().Set("Content-Type", "application/json")
  		json.NewEncoder(w).Encode(Response{Message: "Hello from Bunny 🐰"})
  	})

  	log.Printf("Server starting on port %s", port)
  	log.Fatal(http.ListenAndServe(":"+port, nil))
  }
  ```

  ```go go.mod theme={null}
  module app-go-api

  go 1.23
  ```
</CodeGroup>

## Run locally

Start the development server:

```bash theme={null}
go run main.go
```

Test the API:

```bash theme={null}
curl http://localhost:80/
```

## Create the Dockerfile

```dockerfile Dockerfile theme={null}
FROM golang:1.23-alpine AS builder

WORKDIR /app
COPY go.mod ./
COPY main.go ./
RUN go build -o server .

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/server .
ENV PORT=80
EXPOSE 80
CMD ["./server"]
```

## Build and push to GitHub Container Registry

<Tabs>
  <Tab title="GitHub Actions">
    Create `.github/workflows/build.yml` to automatically build and push on every commit to `main`:

    ```yaml .github/workflows/build.yml theme={null}
    name: Build and Push

    on:
      push:
        branches: [main]

    env:
      REGISTRY: ghcr.io
      IMAGE_NAME: ${{ github.repository }}

    jobs:
      build-and-push:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          packages: write

        steps:
          - uses: actions/checkout@v4

          - name: Log in to GitHub Container Registry
            uses: docker/login-action@v3
            with:
              registry: ${{ env.REGISTRY }}
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}

          - name: Build and push
            uses: docker/build-push-action@v5
            with:
              context: .
              push: true
              tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}

          - name: Update container image on Magic Containers
            uses: BunnyWay/actions/container-update-image@main
            with:
              app_id: ${{ vars.APP_ID }}
              api_key: ${{ secrets.BUNNYNET_API_KEY }}
              container: app
              image_tag: "${{ github.sha }}"
    ```

    Push your code to trigger the workflow:

    ```bash theme={null}
    git init
    git add .
    git commit -m "Initial commit"
    git remote add origin https://github.com/YOUR_USERNAME/app-go-api.git
    git push -u origin main
    ```
  </Tab>

  <Tab title="Docker CLI">
    Build and push manually from your local machine.

    <Steps>
      <Step title="Create a Personal Access Token">
        Go to [GitHub Settings > Developer settings > Personal access tokens](https://github.com/settings/tokens) and create a token with `write:packages` scope.
      </Step>

      <Step title="Log in to GitHub Container Registry">
        ```bash theme={null}
        export CR_PAT=your_personal_access_token
        echo $CR_PAT | docker login ghcr.io -u YOUR_USERNAME --password-stdin
        ```
      </Step>

      <Step title="Build the image">
        ```bash theme={null}
        docker build --platform linux/amd64 -t ghcr.io/YOUR_USERNAME/app-go-api:latest .
        ```

        <Info>
          Magic Containers only supports images built for the **linux/amd64** architecture. The `--platform` flag ensures compatibility regardless of your local machine's architecture.
        </Info>
      </Step>

      <Step title="Push to registry">
        ```bash theme={null}
        docker push ghcr.io/YOUR_USERNAME/app-go-api:latest
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>

<Info>
  If your package is private, set the visibility to **Public** in GitHub or
  [configure Magic Containers with registry
  credentials](/magic-containers/image-registries).
</Info>

## Deploy to Magic Containers

<Steps>
  <Step title="Create a new app">
    In the bunny.net dashboard, go to **Magic Containers** and click **Add
    App**. Enter a name and select your deployment option.
  </Step>

  <Step title="Add a container">
    Click **Add Container**, then configure:

    | Field    | Value                                                                        |
    | -------- | ---------------------------------------------------------------------------- |
    | Registry | GitHub Container Registry                                                    |
    | Image    | `YOUR_USERNAME/{imageName}`                                                  |
    | Tag      | `latest` for Docker CLI, or the commit SHA from your GitHub Actions workflow |
  </Step>

  <Step title="Add an endpoint">
    Go to the **Endpoints** tab, click **Add New Endpoint**, and set the
    container port to `80`.
  </Step>

  <Step title="Deploy">
    Click **Add Container**, then **Next Step**, and **Confirm and Create**.
  </Step>
</Steps>

For more details, see the [quickstart guide](/magic-containers/quickstart).

## Test your API

```bash theme={null}
curl https://mc-xxx.bunny.run/
```

```json Response theme={null}
{ "message": "Hello from Bunny 🐰" }
```

<Info>
  You can [add a custom hostname](/magic-containers/endpoints) from the **Endpoints** section in your app settings.
</Info>

## Connect a database

You can connect your app to [Bunny Database](/database) directly from the dashboard:

1. Go to **Database > \[Your Database] > Access**
2. Click **Generate Tokens**
3. Click **Add Secrets to Magic Container App**
4. Select your app

The `BUNNY_DATABASE_URL` and `BUNNY_DATABASE_AUTH_TOKEN` environment variables are now available in your app:

```go theme={null}
import (
	"database/sql"
	"fmt"
	"os"

	_ "github.com/tursodatabase/libsql-client-go/libsql"
)

url := fmt.Sprintf("%s?authToken=%s",
	os.Getenv("BUNNY_DATABASE_URL"),
	os.Getenv("BUNNY_DATABASE_AUTH_TOKEN"),
)

db, err := sql.Open("libsql", url)
if err != nil {
	fmt.Fprintf(os.Stderr, "failed to open db %s: %s", url, err)
	os.Exit(1)
}
defer db.Close()
```

See the [Go SDK documentation](/database/connect/go) for more details.

## Next steps

1. [Automate deploys with GitHub Actions](/magic-containers/deploy-with-github-actions)
2. [Add a custom hostname](/magic-containers/endpoints)
3. [Add a persistent volume](/magic-containers/persistent-volumes)
