PICTIFY
Go SDK

Go SDK
+ Pictify

Official Go library for high-performance applications.

SDKs & Libraries

About Integration

The Pictify Go SDK is designed for high-performance applications that need efficient image generation. Fully idiomatic Go with context support, proper error handling, and minimal dependencies.

Key Capabilities

Context support

Zero dependencies

Concurrent-safe

Efficient memory use

Common Use Cases

High-throughput APIs

Microservices

CLI tools

Backend services

Integration Guide

5 minutes

Prerequisites

  • Go 1.19 or higher
  • A Pictify account with an API key
  • A template created in Pictify
1

Install the SDK

Add the Pictify Go SDK to your project using go get.

Code
go get github.com/pictify/pictify-go
2

Initialize the Client

Create a new Pictify client with your API key.

Code
package main

import (
    "os"
    "github.com/pictify/pictify-go"
)

func main() {
    // Create client with API key from environment
    client := pictify.NewClient(os.Getenv("PICTIFY_API_KEY"))

    // Or with options
    client := pictify.NewClient(
        os.Getenv("PICTIFY_API_KEY"),
        pictify.WithTimeout(30 * time.Second),
        pictify.WithRetries(3),
    )
}
!

The client is safe for concurrent use. Create one client and reuse it.

3

Generate an Image

Use the Render method to generate images. Always pass a context for cancellation support.

Code
import (
    "context"
    "fmt"
    "github.com/pictify/pictify-go"
)

func generateImage(client *pictify.Client) error {
    ctx := context.Background()

    result, err := client.Render(ctx, &pictify.RenderRequest{
        TemplateID: "your-template-id",
        Variables: map[string]interface{}{
            "title":    "Hello from Go",
            "subtitle": "High-performance image generation",
            "color":    "#667eea",
        },
        Format: pictify.FormatPNG,
        Width:  1200,
        Height: 630,
    })
    if err != nil {
        return fmt.Errorf("render failed: %w", err)
    }

    fmt.Printf("Image URL: %s\n", result.ImageURL)
    return nil
}
4

Download Image as Bytes

Get the raw image bytes for further processing or saving to disk.

Code
func downloadImage(client *pictify.Client) error {
    ctx := context.Background()

    imageBytes, err := client.RenderBytes(ctx, &pictify.RenderRequest{
        TemplateID: "your-template-id",
        Variables: map[string]interface{}{
            "title": "Download Test",
        },
        Format: pictify.FormatPNG,
    })
    if err != nil {
        return err
    }

    // Save to file
    return os.WriteFile("output.png", imageBytes, 0644)
}
5

Concurrent Image Generation

Generate multiple images concurrently using goroutines.

Code
import (
    "context"
    "sync"
)

func generateBatch(client *pictify.Client, items []Item) ([]string, error) {
    ctx := context.Background()
    results := make([]string, len(items))
    errs := make([]error, len(items))

    var wg sync.WaitGroup
    // Limit concurrency with a semaphore
    sem := make(chan struct{}, 5)

    for i, item := range items {
        wg.Add(1)
        go func(idx int, item Item) {
            defer wg.Done()
            sem <- struct{}{}        // Acquire
            defer func() { <-sem }() // Release

            result, err := client.Render(ctx, &pictify.RenderRequest{
                TemplateID: "your-template-id",
                Variables: map[string]interface{}{
                    "title": item.Title,
                    "price": item.Price,
                },
            })
            if err != nil {
                errs[idx] = err
                return
            }
            results[idx] = result.ImageURL
        }(i, item)
    }

    wg.Wait()

    // Check for errors
    for _, err := range errs {
        if err != nil {
            return nil, err
        }
    }
    return results, nil
}
!

Use a semaphore channel to limit concurrent requests and avoid overwhelming the API.

6

Context Timeout and Cancellation

Use context for timeouts and graceful cancellation.

Code
func renderWithTimeout(client *pictify.Client) error {
    // Create context with timeout
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()

    result, err := client.Render(ctx, &pictify.RenderRequest{
        TemplateID: "your-template-id",
        Variables: map[string]interface{}{
            "title": "Timeout Test",
        },
    })
    if err != nil {
        if errors.Is(err, context.DeadlineExceeded) {
            return fmt.Errorf("render timed out")
        }
        return err
    }

    fmt.Println(result.ImageURL)
    return nil
}
7

Error Handling

Handle specific error types for better error recovery.

Code
result, err := client.Render(ctx, req)
if err != nil {
    var apiErr *pictify.APIError
    if errors.As(err, &apiErr) {
        switch apiErr.Code {
        case pictify.ErrTemplateNotFound:
            log.Printf("Template %s not found", req.TemplateID)
        case pictify.ErrRateLimitExceeded:
            log.Printf("Rate limited, retry after %v", apiErr.RetryAfter)
        case pictify.ErrInvalidVariables:
            log.Printf("Invalid variables: %s", apiErr.Message)
        default:
            log.Printf("API error: %s", apiErr.Message)
        }
    }
    return err
}

Fast Install

$ go get github.com/pictify/pictify-go

Ready to build with Go SDK?

Get your API key in seconds and start generating images programmatically.

Related Integrations