Skip to content

Quickstart

Website screenshots in Go

Use net/http to create a screenshot with one HTTPS request and no browser infrastructure to manage.

Ironfang Render uses a standard HTTPS API, so Go can call it with net/http and does not require an SDK. A successful request returns the image bytes directly, ready to write to a file.

Get a key

Create an API key in the portal. The secret is displayed once and stored as a hash. Keep it in an environment variable named IRONFANG_API_KEY rather than in the source file; the example below reads it from there.

export IRONFANG_API_KEY="if_live_..."

Take a screenshot

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

func main() {
	key := os.Getenv("IRONFANG_API_KEY")
	if key == "" {
		log.Fatal("Set IRONFANG_API_KEY to your Ironfang API key.")
	}

	body, err := json.Marshal(map[string]any{
		"url":   "https://example.com",
		"width": 1280,
	})
	if err != nil {
		log.Fatal(err)
	}

	req, err := http.NewRequest(http.MethodPost,
		"https://api.ironfang.com/render/v1/screenshot",
		bytes.NewReader(body))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Authorization", "Bearer "+key)
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{Timeout: 60 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		var e struct {
			Error struct{ Code, Message string } `json:"error"`
		}
		if err := json.NewDecoder(resp.Body).Decode(&e); err != nil {
			log.Fatalf("%d: unreadable error body: %v", resp.StatusCode, err)
		}
		log.Fatalf("%d %s: %s", resp.StatusCode, e.Error.Code, e.Error.Message)
	}

	out, err := os.Create("shot.png")
	if err != nil {
		log.Fatal(err)
	}
	if _, err := io.Copy(out, resp.Body); err != nil {
		log.Fatal(err)
	}
	if err := out.Close(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("saved shot.png")
}

Run it, and the screenshot is saved as shot.png in the current directory:

go run main.go

Handling errors

Successful render responses are binary. Errors use JSON in the form {"error": {"code", "message"}}. A missing or unrecognised key returns 401 invalid_api_key. A key without the render:render scope returns 403 insufficient_scope.

// io.Copy streams the response to disk without buffering the
// complete image in memory. Close is checked because a failed
// close can mean the file was not fully written.

PDFs and templates

The same call shape works for PDFs at /render/v1/pdf, and for templated images at /render/v1/image/{id}. Only the path and the body change.

Next