Document Stack
Document Stack
Docs

Go SDK

Generate PDFs from your Go applications with a lightweight, idiomatic client.

Installation

Bash
go get github.com/documentstack/sdk-go

Quick Start

Go
package main

import (
    "fmt"
    "os"

    documentstack "github.com/documentstack/sdk-go"
)

func main() {
    client := documentstack.NewClient(os.Getenv("DOCUMENTSTACK_API_KEY"))

    pdf, err := client.Generate(documentstack.GenerateRequest{
        TemplateID: "tmpl_abc123",
        Data: map[string]interface{}{
            "customer_name":  "Acme Corp",
            "invoice_number": "INV-001",
            "total":          "$5,000.00",
        },
    })
    if err != nil {
        fmt.Fprintf(os.Stderr, "Generation failed: %v\n", err)
        os.Exit(1)
    }

    os.WriteFile("invoice.pdf", pdf, 0644)
    fmt.Println("PDF generated successfully!")
}

Configuration

OptionTypeDefaultDescription
apiKeystringYour API key (required, passed to NewClient)
BaseURLstringAPI URLCustom URL for self-hosted instances
Timeouttime.Duration30sRequest timeout
Go
client := documentstack.NewClient(
    os.Getenv("DOCUMENTSTACK_API_KEY"),
    documentstack.WithBaseURL("https://your-instance.com"),
    documentstack.WithTimeout(60 * time.Second),
)

HTTP Handler Example

Serve generated PDFs from an HTTP handler:

Go
package main

import (
import { CodeBlock } from "@/components/docs/CodeBlock";
import { generateDocsMetadata, generateDocsJsonLd } from "@/lib/docs-seo";

    "log"
export const metadata = generateDocsMetadata("/docs/sdk-go");

    "net/http"
    "os"

    documentstack "github.com/documentstack/sdk-go"
)

var client = documentstack.NewClient(os.Getenv("DOCUMENTSTACK_API_KEY"))

func invoicePDFHandler(w http.ResponseWriter, r *http.Request) {
    invoiceID := r.PathValue("id")
    invoice, err := getInvoice(invoiceID)
    if err != nil {
        http.Error(w, "Invoice not found", http.StatusNotFound)
        return
    }

    pdf, err := client.Generate(documentstack.GenerateRequest{
        TemplateID: "tmpl_invoice",
        Data: map[string]interface{}{
            "customer_name":  invoice.CustomerName,
            "invoice_number": invoice.Number,
            "total":          invoice.Total,
        },
    })
    if err != nil {
        http.Error(w, "PDF generation failed", http.StatusInternalServerError)
        return
    }

    w.Header().Set("Content-Type", "application/pdf")
    w.Header().Set("Content-Disposition", "attachment; filename=\"invoice.pdf\"")
    w.Write(pdf)
}

func main() {
    http.HandleFunc("GET /invoices/{id}/pdf", invoicePDFHandler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

Gin Example

Go
r.GET("/invoices/:id/pdf", func(c *gin.Context) {
    pdf, err := client.Generate(documentstack.GenerateRequest{
        TemplateID: "tmpl_invoice",
        Data: map[string]interface{}{
            "customer_name": "Acme Corp",
            "total":         "$5,000.00",
        },
    })
    if err != nil {
        c.JSON(500, gin.H{"error": "PDF generation failed"})
        return
    }

    c.Header("Content-Disposition", "attachment; filename=invoice.pdf")
    c.Data(200, "application/pdf", pdf)
})

Error Handling

Go
pdf, err := client.Generate(req)
if err != nil {
    var apiErr *documentstack.APIError
    if errors.As(err, &apiErr) {
        fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Message)
    } else {
        fmt.Printf("Network error: %v\n", err)
    }
}
The Go SDK follows Go conventions. Errors are returned as the second return value, and the client is safe for concurrent use from multiple goroutines.

View on GitHub

The source code is available on GitHub. Report issues, view examples, or contribute improvements.