Skip to content

Official TemplateFox Go SDK - Generate PDFs from HTML templates via API

License

Notifications You must be signed in to change notification settings

TemplateFoxPDF/gosdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TemplateFox Go SDK

Official Go SDK for TemplateFox - Generate PDFs from HTML templates via API.

Go Reference License: MIT

Installation

go get github.com/TemplateFoxPDF/gosdk

Quick Start

package main

import (
    "context"
    "fmt"
    "log"

    templatefox "github.com/TemplateFoxPDF/gosdk"
)

func main() {
    // Initialize the client
    config := templatefox.NewConfiguration()
    config.AddDefaultHeader("x-api-key", "your-api-key")

    client := templatefox.NewAPIClient(config)
    ctx := context.Background()

    // Generate a PDF
    request := templatefox.CreatePdfRequest{
        TemplateId: "YOUR_TEMPLATE_ID",
        Data: map[string]interface{}{
            "name":           "John Doe",
            "invoice_number": "INV-001",
            "total_amount":   150.00,
        },
    }

    response, _, err := client.PDFApi.CreatePdf(ctx, request)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("PDF URL: %s\n", *response.Url)
    fmt.Printf("Credits remaining: %d\n", *response.CreditsRemaining)
}

Features

  • Template-based PDF generation - Create templates with dynamic variables, generate PDFs with your data
  • Multiple export options - Get a signed URL (default) or raw binary PDF
  • S3 integration - Upload generated PDFs directly to your own S3-compatible storage
  • Context support - Full context.Context support for cancellation and timeouts

API Methods

PDF Generation

request := templatefox.CreatePdfRequest{
    TemplateId: "TEMPLATE_ID",
    Data: map[string]interface{}{
        "name": "John Doe",
    },
    ExportType: templatefox.PtrString("url"),  // "url" or "binary"
    Expiration: templatefox.PtrInt32(86400),   // URL expiration in seconds
    Filename:   templatefox.PtrString("invoice-001"),
}

response, _, err := client.PDFApi.CreatePdf(ctx, request)

Templates

// List all templates
templates, _, err := client.TemplatesApi.ListTemplates(ctx)
for _, t := range templates.Templates {
    fmt.Printf("%s: %s\n", t.Id, t.Name)
}

// Get template fields
fields, _, err := client.TemplatesApi.GetTemplateFields(ctx, "TEMPLATE_ID")
for _, f := range fields {
    fmt.Printf("%s: %s (required: %t)\n", f.Key, f.Type, f.Required)
}

Account

// Get account info
account, _, err := client.AccountApi.GetAccount(ctx)
fmt.Printf("Credits: %d\n", account.Credits)
fmt.Printf("Email: %s\n", *account.Email)

// List transactions (with optional params)
transactions, _, err := client.AccountApi.ListTransactions(ctx).
    Limit(100).
    Offset(0).
    Execute()
for _, tx := range transactions.Transactions {
    fmt.Printf("%s: %d credits\n", tx.TransactionType, tx.Credits)
}

S3 Integration

// Save S3 configuration
s3Config := templatefox.S3ConfigRequest{
    EndpointUrl:     "https://s3.amazonaws.com",
    AccessKeyId:     "AKIAIOSFODNN7EXAMPLE",
    SecretAccessKey: templatefox.PtrString("your-secret-key"),
    BucketName:      "my-pdf-bucket",
    DefaultPrefix:   templatefox.PtrString("generated/pdfs/"),
}

_, _, err := client.IntegrationsApi.SaveS3Config(ctx, s3Config)

// Test connection
test, _, err := client.IntegrationsApi.TestS3Connection(ctx)
fmt.Printf("Connection: %s\n", test.Message)

Configuration

config := templatefox.NewConfiguration()
config.Servers = templatefox.ServerConfigurations{
    {URL: "https://api.pdftemplateapi.com"},
}
config.AddDefaultHeader("x-api-key", os.Getenv("TEMPLATEFOX_API_KEY"))

client := templatefox.NewAPIClient(config)

Error Handling

response, httpResp, err := client.PDFApi.CreatePdf(ctx, request)

if err != nil {
    if httpResp != nil {
        switch httpResp.StatusCode {
        case 402:
            log.Println("Insufficient credits")
        case 403:
            log.Println("Access denied - check your API key")
        case 404:
            log.Println("Template not found")
        default:
            log.Printf("Error: %v", err)
        }
    } else {
        log.Printf("Error: %v", err)
    }
    return
}

Documentation

Support

License

MIT License - see LICENSE for details.