Go SDK
Use the official Pritset Go SDK to manage DOCX templates and generate PDFs from your Go application.
View the official Go SDK repository on GitHub
Requirementsโ
- Go 1.25 or newer
- A Pritset access token and secret from the Profile page
The SDK uses only the Go standard library at runtime.
Compatibilityโ
This page documents SDK version 0.1.5, which implements Pritset SDK contract 1.0.0.
Install the moduleโ
Install the v0.1.5 release:
go get github.com/pritset/[email protected]
Create a clientโ
Store credentials in environment variables rather than source code. Pritset sends the access token directly in the Authorization header and the secret in X-Secret; do not add a Bearer prefix.
package main
import (
"log"
"os"
pritset "github.com/pritset/pritset-go-sdk"
)
func main() {
client, err := pritset.NewClient(
os.Getenv("PRITSET_ACCESS_TOKEN"),
os.Getenv("PRITSET_SECRET"),
)
if err != nil {
log.Fatal(err)
}
_ = client
}
The client's String and GoString methods redact credentials. Do not commit or log either credential.
Generate a PDFโ
Upload a DOCX template first and copy its ID. See Template management for upload requirements and the portal workflow.
All SDK operations accept a context.Context. PDF responses stream through io.ReadCloser; always close the response body.
ctx := context.Background()
response, err := client.Documents().Generate(ctx, "YOUR_TEMPLATE_ID", map[string]any{
"invoice": map[string]any{
"number": "INV-1042",
"customer": "Ada Lovelace",
},
})
if err != nil {
return err
}
defer response.Body.Close()
output, err := os.Create("invoice.pdf")
if err != nil {
return err
}
defer output.Close()
_, err = io.Copy(output, response.Body)
return err
Response metadata is available as ContentType, ContentLength, and Trace. The value passed to Generate becomes the template data, so its shape must match the placeholders and loops in your DOCX file. See Templating syntax.
Manage templatesโ
Use client.Templates() to list, retrieve, create, update, download, validate, and delete templates.
ascending := 0
page, err := client.Templates().List(ctx, &pritset.ListTemplatesOptions{
Query: "invoice",
Page: 1,
PageSize: 25,
SortBy: "name",
SortDirection: &ascending, // 0 ascending, 1 descending
})
if err != nil {
return err
}
for _, template := range page.Data {
fmt.Println(template.ID, template.Name)
}
upload, err := pritset.OpenUpload(
"invoice.docx",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)
if err != nil {
return err
}
defer upload.Close()
tags := "invoice,monthly"
created, err := client.Templates().Create(ctx, pritset.CreateTemplateParams{
Name: "Monthly invoice",
Tags: &tags,
Template: upload,
})
if err != nil {
return err
}
Retrieve the complete template record, including file metadata, then update its name and tags. Template is optional in UpdateTemplateParams; set it to an upload to replace the DOCX file.
details, err := client.Templates().Get(ctx, created.ID)
if err != nil {
return err
}
fmt.Println(details.FileInfo.ObjectName, details.FileInfo.Size)
replacement, err := pritset.OpenUpload("invoice-2026.docx", "")
if err != nil {
return err
}
defer replacement.Close()
updated, err := client.Templates().Update(ctx, created.ID, pritset.UpdateTemplateParams{
Name: "Monthly invoice 2026",
Tags: &tags,
Template: replacement, // omit for a metadata-only update
})
if err != nil {
return err
}
_ = updated
OpenUpload opens a file-backed upload, so call Close after the request. NewUpload accepts any io.Reader; the caller owns that reader, and Upload.Close closes it only when it implements io.Closer.
download, err := client.Templates().Download(ctx, created.ID)
if err != nil {
return err
}
defer download.Body.Close()
destination, err := os.Create("downloaded-invoice.docx")
if err != nil {
return err
}
defer destination.Close()
if _, err := io.Copy(destination, download.Body); err != nil {
return err
}
validationUpload, err := pritset.OpenUpload("invoice.docx", "")
if err != nil {
return err
}
defer validationUpload.Close()
valid, err := client.Templates().Validate(ctx, validationUpload, map[string]any{
"invoice": map[string]any{"number": "INV-1042"},
})
if err != nil {
return err
}
fmt.Printf("Template valid: %t\n", valid)
return client.Templates().Delete(ctx, created.ID)
For endpoint behavior and limits, see the Template management API reference.
Raw JSONโ
Use json.RawMessage when data is already encoded. Invalid JSON is rejected before a request is sent.
data := json.RawMessage(`{"name":"Ada"}`)
response, err := client.Documents().Generate(ctx, "YOUR_TEMPLATE_ID", data)
Generate through a webhookโ
Use GenerateWebhook when Pritset should send the generation result to your endpoint. The webhook URL must be an absolute HTTP(S) URL without embedded credentials.
job, err := client.Documents().GenerateWebhook(
ctx,
"YOUR_TEMPLATE_ID",
map[string]any{"invoice": map[string]any{"number": "INV-1042"}},
"https://example.com/webhooks/pritset",
)
if err != nil {
return err
}
fmt.Println(job.ID)
The SDK submits the URL to Pritset; it does not call the webhook itself. See Direct and webhook processing for the API workflow.
Handle errors and cancellationโ
The SDK returns *pritset.APIError for API responses with an error status and *pritset.TransportError for timeouts, cancellation, and transport failures. It does not retry automatically.
response, err := client.Documents().Generate(ctx, "YOUR_TEMPLATE_ID", map[string]any{
"name": "Ada Lovelace",
})
if err == nil {
defer response.Body.Close()
}
var apiError *pritset.APIError
var transportError *pritset.TransportError
switch {
case errors.As(err, &apiError):
fmt.Printf("HTTP %d; trace ID: %s\n", apiError.StatusCode, apiError.TraceID)
case errors.As(err, &transportError):
fmt.Printf("Timed out: %t; canceled: %t\n", transportError.Timeout, transportError.Canceled)
case err != nil:
return err
}
Control cancellation and request lifetime through the context you pass to each operation. See API errors for status codes and retry guidance.
Local development and custom HTTP clientsโ
The SDK uses https://api.pritset.com by default and disables redirects on the HTTP client it uses. HTTP is allowed only for exact loopback development endpoints such as http://127.0.0.1:8080.
When you supply an http.Client, the SDK makes a shallow clone, does not mutate the original client, and disables redirects on the clone:
httpClient := &http.Client{Timeout: 60 * time.Second}
client, err := pritset.NewClient(
token,
secret,
pritset.WithHTTPClient(httpClient),
pritset.WithTimeout(45*time.Second),
)
if err != nil {
return err
}