Guide · Go
Generate PDFs from a CSV in Go
A list of people and a template make a stack of documents. Instead of one request per row, a batch queues up to 1,000 renders as one job, and one zip comes back. This Go program reads a CSV, maps the rows and waits for the job.
Free plan with 100 units a month, no credit card
What you need
- An API key. Create one in the app under API keys and export it as
FORMFEED_API_KEY. A test key (ff_test_…) is right for trying things out. See the quick start. - Go. You need Go 1.21 or newer; the program uses the standard library only.
- The template.
certificatefrom the certificate example, whose fields are exactly the columns of the CSV. - The CSV.
attendees.csvnext to the program, with a header row. It is shown below. - Enough units. Batches are part of every plan, and each item counts as at least one unit. On the Free plan a live batch is refused when the month has fewer units left than the batch has items; a test key is not counted. The limits page has the units per plan.
The program
The program reads the CSV, turns every row into an item, queues them as one job with zip: true, waits until the job is done and saves the archive.
package main
import (
"bytes"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"time"
)
const base = "https://api-eu.formfeed.dev/v1"
type job struct {
ID string `json:"id"`
Status string `json:"status"`
Total int `json:"total"`
Succeeded int `json:"succeeded"`
Failed int `json:"failed"`
ZipURL string `json:"zip_url"`
Title string `json:"title"`
Detail string `json:"detail"`
}
func api(method, path string, body any) job {
var reader io.Reader
if body != nil {
encoded, _ := json.Marshal(body)
reader = bytes.NewReader(encoded)
}
request, _ := http.NewRequest(method, base+path, reader)
request.Header.Set("Authorization", "Bearer "+os.Getenv("FORMFEED_API_KEY"))
request.Header.Set("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
var answer job
json.NewDecoder(response.Body).Decode(&answer)
if response.StatusCode >= 400 {
fmt.Fprintf(os.Stderr, "%s: %s\n", answer.Title, answer.Detail)
os.Exit(1)
}
return answer
}
func main() {
file, err := os.Open("attendees.csv")
if err != nil {
panic(err)
}
rows, err := csv.NewReader(file).ReadAll()
file.Close()
if err != nil {
panic(err)
}
items := []any{}
for _, row := range rows[1:] {
// every field of a CSV is text: the template expects hours as a number
hours, _ := strconv.Atoi(row[3])
items = append(items, map[string]any{
"data": map[string]any{"name": row[0], "course": row[1], "completed_on": row[2], "hours": hours},
})
}
queued := api("POST", "/renders/batch", map[string]any{"template": "certificate", "zip": true, "items": items})
fmt.Println(queued.ID, queued.Total, "queued")
// the job is always asynchronous: ask for it until it is no longer queued or processing
done := queued
for done.Status == "queued" || done.Status == "processing" {
time.Sleep(2 * time.Second)
done = api("GET", "/jobs/"+queued.ID, nil)
}
fmt.Printf("%d/%d rendered, %d failed\n", done.Succeeded, done.Total, done.Failed)
if done.ZipURL != "" {
zip, err := http.Get(done.ZipURL)
if err != nil {
panic(err)
}
defer zip.Body.Close()
out, _ := os.Create("certificates.zip")
defer out.Close()
io.Copy(out, zip.Body)
}
}Run it
It prints the job id and how many renders were queued, then 4/4 rendered, 0 failed, and certificates.zip lies next to the program with one PDF per row, 0001-document.pdf to 0004-document.pdf.
export FORMFEED_API_KEY=ff_test_...
go run main.goWhat is particular about Go
json.RawMessage. The sample data goes into the body as it is on disk, without being decoded into Go types first, so the program works for any template.- One struct for both answers. A render and a problem document are both JSON objects, so a single struct with
titleanddetailbeside the render fields decodes either. - Timeouts.
http.DefaultClienthas none. In a service, give the client aTimeouta little above 60 seconds: a synchronous render answers when the file is done, or after 60 seconds with202while it goes on in the background.
The CSV
One header row, one line per document. The columns are the fields the template expects, so no mapping table is needed:
name,course,completed_on,hours
Erika Mustermann,Analytical Engines 101,2026-09-10,16
Max Mustermann,Analytical Engines 101,2026-09-10,16
Jane Doe,Print CSS for developers,2026-09-11,8
John Doe,Print CSS for developers,2026-09-11,8What a batch does differently
- Always asynchronous. The answer comes back at once with the status
queued. The renders run in the background, as many at a time as your plan allows. - One template, many items. A top-level
templateapplies to every item that does not name its own, so an item is usually just itsdata. - Partly failed is a state. A job in which some items failed ends as
partially_failed(failedwhen all did).succeededandfailedcount them, and every item is a render of its own initems, with its own error. - The zip is optional. Every item keeps its own download URL in
items, which is what you want when the documents go to different people.zip: trueadds one archive of the succeeded ones, each named0001-and its item’sfilename(document.pdfwithout one), in row order. - Or a webhook. Instead of waiting, give the batch a
webhook_urland letjob.completed(orjob.failed, when every item failed) tell you. For thousands of documents that is the better shape: no process waiting for minutes.
When the API says no
- One shape for every error. Errors are problem documents (RFC 9457) with
type,title,status,detail, a stablecodeand therequest_idto quote to support; thetypeURL leads to a page about that error. The list is in the error reference. - The usual ones.
401 unauthorizedfor a missing or revoked key,404 template_not_foundfor a wrong template name,422 template_runtime_errorwhen the template fails on the data,429 rate_limitedabove the plan’s requests per second,402 quota_exceededwhen the Free plan’s units are used up (a paid plan bills extra units up to its hard cap). - Retry safely. Send an
Idempotency-Keyheader with the POST, and repeat the request with the same key when you do not know whether the first attempt arrived: for 24 hours the key returns the first answer instead of rendering and billing again. The answer is stored once the first attempt has been answered, so do not retry while it may still run; and problems below 500 are stored too, so a corrected request needs a new key.
The job around this program
This guide is one program. What the job needs around it, from the template to the limits and the price, is on the page Certificate generator API.
More Go guides
FAQ
- How many rows can one job have?
- Up to 1,000 items per batch. Split a longer list into several jobs; there is no limit on how many jobs you run, and each counts once against the rate limit. See async and batch.
- What happens when one row is wrong?
- When the template fails on a row’s data, that item fails and the others still render: the job ends as
partially_failedand the failed item carries the error, so you can fix the row and render it again on its own. An item the API refuses outright (a request field it does not accept, or data that misses the template’s schema undervalidate_data) refuses the whole batch before anything is queued, and the problem names the item’s position. - Does a failed item cost units?
- No. Only documents that were produced are counted.
- Can I send a spreadsheet instead of writing code?
- Yes: the app takes a CSV or Excel file, maps the columns to the template’s fields and runs the batch for you. The code here is for the same thing inside your own system.