# Go SDK

> Use the Nullgate Go SDK to generate Akamai, Amazon and ThreatMetrix requests or classify images. Install from Git and keep your own HTTP client.

Use `solver.Client` to solve supported flows through the local HTTP API. The server sends target requests and holds the API key.

## Simple mode

[Start the local API](https://nullgate.club/en/start) and install the Go SDK. Set `TARGET_URL` to a supported target page and `PROXY_URL` to your proxy.

```go
package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/nullgate-club/nullgate-sdk-go/v2/solver"
)

func main() {
    client, err := solver.New(solver.Options{})
    if err != nil { log.Fatal(err) }
    response, err := client.Solve(context.Background(), solver.Request{
        Type: solver.Akamai, URL: os.Getenv("TARGET_URL"),
        UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
        Proxy: os.Getenv("PROXY_URL"), Session: "visit-1",
    })
    if err != nil { log.Fatal(err) }
    fmt.Println(response.Cookies, response.Result.Accepted)
}
```

`Cookies` retains cookie attributes. `Result` contains typed product fields. Use the same user agent and proxy for subsequent site requests. The named session pins those settings and transport. Pass refreshed `Cookies` to replace the origin's snapshot after external changes.

Other types are `solver.ThreatMetrix`, `solver.PingOne` and `solver.AmazonACIC`. PingOne returns data rather than a login or cookie. ACIC completes PoW, WAF, voucher and continuation through `PASS`. If the script cannot be discovered, supply its real URL in `ScriptURL`. Pass an already received page in `HTML` to avoid fetching it again. Unsupported cases return an error. One solve can incur multiple generation charges at the existing [prices](https://nullgate.club/en/prices). The local server adds no separate charge.

## Advanced: direct API and prepared requests

The following direct API uses a key in the Go process. It prepares requests for your own target client instead of running the local simple-mode flow.

### Install and check your account

Go 1.24.1 or newer. Get an API key from [@nullgate_app](https://t.me/nullgate_app).

```sh
GOPROXY=direct go get github.com/nullgate-club/nullgate-sdk-go/v2@main
export NULLGATE_API_KEY=ng_your_key
```

```go
package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "github.com/nullgate-club/nullgate-sdk-go/v2"
    "github.com/nullgate-club/nullgate-sdk-go/v2/api"
)

func main() {
    client, err := nullgate.New("", api.Options{})
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()
    account, err := client.Accounts.Get(ctx)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Balance: %s %s\n", account.Balance, account.Currency)
}
```

An empty key reads `NULLGATE_API_KEY`. Get usage buckets with `client.Accounts.Usage(ctx, accounts.UsageOptions{})`. Amounts and prices are decimal strings, not `float64`. Account reads are free. See [Keys and balance](https://nullgate.club/en/account) and [Prices](https://nullgate.club/en/prices).

## Choose an operation

| Product | Guide and runnable example |
| --- | --- |
| Akamai | [Akamai](https://nullgate.club/en/akamai) · [Sensor and pixel example](https://github.com/nullgate-club/nullgate-sdk-go/tree/main/examples/sensor) |
| Amazon FWCIM | [Amazon FWCIM](https://nullgate.club/en/amazon-fwcim) · [Form example](https://github.com/nullgate-club/nullgate-sdk-go/tree/main/examples/amazon) |
| Amazon ACIC | [ACIC](https://nullgate.club/en/amazon-acic) · [PoW/WAF example](https://github.com/nullgate-club/nullgate-sdk-go/tree/main/examples/acic) |
| ThreatMetrix | [ThreatMetrix](https://nullgate.club/en/threatmetrix) · [Callback example](https://github.com/nullgate-club/nullgate-sdk-go/tree/main/examples/threatmetrix) |
| Images | [Classifier](https://nullgate.club/en/classifier) · [Image example](https://github.com/nullgate-club/nullgate-sdk-go/tree/main/examples/classifier) |

For labels, call `client.Classifier.Classify` with `classifier.ClassifyOptions`. For queries, call `client.Classifier.Match` with `classifier.MatchOptions`. Match scores are independent.

## Keep the visit together

1. Create a browser identity with `browser.New(browser.Options{UserAgent: ua})`. Reuse it for the visit.
2. Call `visit.Page(url, html, browser.PageOptions{})` for each navigation, using its final URL and current HTML.
3. Create a product session with `client.Akamai.Session(visit)` and reuse it for that visit. Browser and product sessions are separate. A product session rejects a page from another visit.
4. Discover URLs with `akamai.SensorURL(page)`, `akamai.PixelURL(page)`, or `akamai.SbsdURL(page)`. An empty result means none was found. Ambiguous discovery returns an error.
5. Pass cookies from the target client's jar. Call `session.Sensor(ctx, page, akamai.SensorOptions{...})`, `session.Pixel(ctx, page, akamai.PixelOptions{...})`, or `session.Sbsd(ctx, page, akamai.SbsdOptions{...})`.
6. Call `result.Wait(ctx)`, then send the request through your target client.

Use `page.Inputs` to summarize ordinary field values from a map. Use `browser.PrepareInputs([]browser.Input{...})` for explicit summaries, including passwords. Never put raw password values in page HTML or actions.

For FWCIM, create and reuse `client.Amazon.Session(visit)` and call `session.Fwcim(ctx, page, amazon.FwcimOptions{...})`. Use `amazon.PrepareInputs(page, []browser.Input{...})` while raw values are available. It returns `amazon.Input` values: browser summaries plus FWCIM-only metadata. It computes the source-compatible checksum and supplies email text for the canvas probe. Passwords keep only summaries. If you supply precomputed observations, nonempty submitted nonpassword fields need a checksum, and the email field also needs its text.

For ACIC, use `acic.Discover`, `acic.ParseResult`, and `acic.DiscoverWaf` from `amazon/acic`. Use their setup methods and send the resulting requests with your target client. Call `client.Amazon.ACIC.Pow` or `client.Amazon.ACIC.Waf` to generate solutions.

For ThreatMetrix, create and reuse `client.ThreatMetrix.Session(visit)`, then call `session.Start(ctx, page, threatmetrix.Options{...})` with a target fetch callback. Keep the startup context alive for the profile lifetime. `WaitCollected` waits for collection, `Pagehide` signals departure, and `Close` stops the profile.

**Omitted and empty are different.** A nil `Inputs` or `Actions` slice keeps automatic collection. A non-nil empty slice explicitly disables that collection.

Keep passing the downloaded script to `session.Sensor`. The SDK retains its digest after the server confirms retention. Later requests omit the script, and `script_required` triggers one retry with the original text.

The SDK gzips JSON bodies of at least 1,024 bytes. Classifier images remain raw multipart bytes. `api.Options.HTTPClient` configures API traffic only. The SDK copies it and disables its cookies and redirects. Keep target cookies and proxy settings in your own client. Go's `net/http` does not preserve wire header order or reproduce Chrome's TLS fingerprint.

## Handle errors and cancellation

Every network operation accepts `context.Context`. Cancel it to stop a request or active profile. `Close` stops the client and its profiles.

Use `errors.As` for `*api.Error` fields: `Code`, `Message`, `Status`, `RequestID`. Error text includes `Message`, not `Cause` or credentials. `errors.Is` matches `api.ErrClosed` and underlying context cancellation or deadline errors.

Only explicit maintenance rejection and script recovery trigger bounded retries. ACIC generation never retries automatically. `Report` confirms HTTP delivery of a request ID, not target acceptance or a refund.

See [Errors](https://nullgate.club/en/errors), [Sessions](https://nullgate.club/en/sessions), and the [SDK README](https://github.com/nullgate-club/nullgate-sdk-go).

## Migrate from v1

The old SDK remains in the repository's `v1` branch. API v1 hosts are off. Import `/v2` and migrate from `Open` and `Solve` to the browser and product sessions above. The root package has no session, options, error, or model aliases.

If you used the initial flat v2 API, replace `nullgate.NewClient` with `nullgate.New`, and use `api.Options` and `api.Error`. Replace `client.Session` and page-level product methods with the browser visit and product sessions shown above. No compatibility aliases remain.
