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

# Go SDK

> Learn how to generate images and PDFs with the Bannerify Go SDK.

Use `bannerify-go` to call the Bannerify API from Go services without writing raw HTTP requests.

## Install

Install via Go modules:

```bash theme={null}
go get github.com/bannerify/bannerify-go
```

## Instantiate

You need a project API key before using the SDK. Create one in **Dashboard → API Keys** and keep it server-side.

```go theme={null}
package main

import (
    "github.com/bannerify/bannerify-go"
)

func main() {
    client := bannerify.NewBannerifyClient("bnfy_xxx")
}
```

## Response format

To make error handling explicit and prevent forgotten error checks, we return errors as part of the response structure. Every method returns a `Response` struct with either a `Result` or an `Error` field, never both and never none.

<CodeGroup titles={["Success", "Error"]}>
  ```go theme={null}
  type Response struct {
      Result interface{} // The result (e.g., []byte for image data)
      Error  *ErrorResponse
  }
  ```

  ```go theme={null}
  type ErrorResponse struct {
      Error struct {
          Code    string // Machine readable error code
          Message string // Human readable message
          Docs    string // Link to documentation
      }
  }
  ```
</CodeGroup>

## Checking for errors

To check for errors, use the `Error` field in the response:

```go theme={null}
package main

import (
    "context"
    "fmt"
    "os"
    
    "github.com/bannerify/bannerify-go"
)

func main() {
    client := bannerify.NewBannerifyClient("your-api-key")
    
    result := client.CreateImage(context.Background(), "tpl_xxxxxxxxx", &bannerify.CreateImageOptions{
        Modifications: []bannerify.Modification{
            {Name: "title", Text: "Hello World"},
        },
    })
    
    if result.Error != nil {
        // Handle error
        fmt.Printf("Error: %s\n", result.Error.Error.Message)
        return
    }
    
    // Process result
    imageData := result.Result.([]byte)
    os.WriteFile("output.png", imageData, 0644)
}
```

## Options

The client accepts options to customize the behavior:

### API Key

```go theme={null}
client := bannerify.NewBannerifyClient("my-api-key")
```

### Custom Configuration

```go theme={null}
import "github.com/bannerify/bannerify-go/option"

client := bannerify.NewBannerifyClient("my-api-key",
    option.WithBaseURL("https://api.bannerify.co"),
    option.WithMaxRetries(3),
)
```

## Examples

### Generate PNG Image

```go theme={null}
result := client.CreateImage(ctx, "tpl_xxxxxxxxx", &bannerify.CreateImageOptions{
    Modifications: []bannerify.Modification{
        {Name: "title", Text: "Hello World"},
        {Name: "subtitle", Text: "From Go SDK"},
    },
})

if result.Error == nil {
    imageData := result.Result.([]byte)
    os.WriteFile("output.png", imageData, 0644)
}
```

### Generate WebP Image

```go theme={null}
result := client.CreateImage(ctx, "tpl_xxxxxxxxx", &bannerify.CreateImageOptions{
    Format: "webp",
    Modifications: []bannerify.Modification{
        {Name: "title", Text: "Hello WebP"},
    },
})

if result.Error == nil {
    webpData := result.Result.([]byte)
    os.WriteFile("output.webp", webpData, 0644)
}
```

### Generate Signed URL

```go theme={null}
signedURL, err := client.GenerateImageSignedURL("tpl_xxxxxxxxx", &bannerify.CreateImageOptions{
    Modifications: []bannerify.Modification{
        {Name: "title", Text: "Dynamic Title"},
    },
})

if err == nil {
    fmt.Printf("<img src='%s' alt='Generated Image' />", signedURL)
}
```
