> ## Documentation Index
> Fetch the complete documentation index at: https://exchanger-api.iexexchanger.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect to Exchanger API with the Go SDK

> Install the Go SDK for Exchanger API v3, verify your origin and API credentials, handle typed errors, and follow a cursor to the next catalog page.

The Go SDK adds `/api/v3` to your installation URL, signs private requests with HMAC v2, and returns typed results. Start with two ping requests to verify the exchanger's address, token, and signing secret.

## Install the module

Use Go 1.26.0 or later. If your project has no `go.mod`, create one with `go mod init example.com/exchanger-ping`. Then add the verified module revision.

```bash theme={null}
go get github.com/iexexchanger/exchanger-sdk-go@v0.0.0-20261205184333-420365c012c8
```

This command pins the Go SDK to a pseudo-version for a specific commit. Check the [repository tags](https://github.com/iexexchanger/exchanger-sdk-go/tags) when updating the module.

## Verify the address without credentials

Set `EXCHANGER_API_ORIGIN` or replace `https://your-exchange` in the example with your installation's HTTPS origin. Save the code as `main.go` and run `go run .`. On success, `Ping` prints `status=ok` and the exchanger name.

```go main.go theme={null} theme={null}
package main

import (
	"context"
	"log"
	"os"

	exchanger "github.com/iexexchanger/exchanger-sdk-go"
)

func main() {
	origin := os.Getenv("EXCHANGER_API_ORIGIN")
	if origin == "" {
		origin = "https://your-exchange"
	}

	client, err := exchanger.New(exchanger.Config{Origin: origin})
	if err != nil {
		log.Fatal(err)
	}

	ping, err := client.Platform().Ping(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("status=%s site=%s", ping.Result.Status, ping.Result.SiteName)
}
```

## Verify credentials and signing

Set the process environment variables. Get both credentials from the same [API key](https://exchanger-api.iexexchanger.com/en/start/access.md).

| Variable | Value |
| - | - |
| `EXCHANGER_API_ORIGIN` | The installation's HTTPS origin, such as `https://your-exchange`. The SDK adds `/api/v3`. |
| `EXCHANGER_API_TOKEN` | The complete token without the `Bearer ` prefix. |
| `EXCHANGER_API_HMAC_SECRET` | The HMAC secret issued with that token. |

Replace the contents of `main.go` and run `go run .` again.

```go main.go theme={null}
package main

import (
	"context"
	"log"
	"os"
	"time"

	exchanger "github.com/iexexchanger/exchanger-sdk-go"
)

// main checks API credentials by sending a signed request to private ping.
func main() {
	client, err := exchanger.New(exchanger.Config{
		Origin:      os.Getenv("EXCHANGER_API_ORIGIN"),
		BearerToken: os.Getenv("EXCHANGER_API_TOKEN"),
		HMACSecret:  os.Getenv("EXCHANGER_API_HMAC_SECRET"),
		Timeout:     10 * time.Second,
	})
	if err != nil {
		log.Fatalf("configure Exchanger API: %v", err)
	}

	ping, err := client.Platform().PrivatePing(context.Background())
	if err != nil {
		log.Fatalf("private ping: %v", err)
	}
	log.Printf("status=%s request_id=%s", ping.Result.Status, ping.Metadata.RequestID)
}
```

Expect `status=ok`. `PrivatePing` verifies credentials with a fresh HMAC v2 signature and requires no additional scope. `Ping` sends an anonymous request even when credentials are configured. Share `Metadata.RequestID` if you contact the operator.

Construct one client per installation and reuse it across requests. `Config.Timeout` covers the whole request, including reading the response. A zero value defaults to 30 seconds. `Config.Locale` sets the `X-Locale` header for a locale the installation supports. The SDK stops at HTTP redirects.

## Call methods that require scopes

Grant `public-api:exchange:routes:read` to use `client.Exchange().Routes(ctx, query)`. Calling `client.Exchange().Quote(ctx, input)` requires `public-api:exchange:quotes:read`. Look up scopes for other methods in the [API reference](https://exchanger-api.iexexchanger.com/en/api.md).

For the route catalog, pass parameters in `api.Query` from `github.com/iexexchanger/exchanger-sdk-go/api`. To use a cursor, start with `pagination=cursor`. When `Metadata.Pagination` is present, `HasMore` is `true`, and `NextCursor` is set, pass its value in `cursor` and keep the same filters and sort order. [Lists and pagination](https://exchanger-api.iexexchanger.com/en/start/protocol.md#lists-and-pagination)

## Handle errors and retries

Use `errors.As` with typed errors from the `api` package. Check for missing credentials on a private call with `errors.Is(err, api.ErrMissingCredentials)`.

| Type | What to check |
| - | - |
| `*api.APIError` | `Code` is the API error code. `Details` contains JSON details when provided. `Metadata` holds the HTTP status and request ID. |
| `*api.ProtocolError` | The response differs from the API contract. For example, a proxy returned HTML. Check `Reason` and the available `Metadata` fields. |
| `*api.TransportError` | A network or response-reading failure. Use `errors.Is` to compare the underlying error or `errors.As` to extract it by type. |
| `*api.RequestError` | The SDK detected invalid input before dispatch. Check `Reason`. |

Your application decides how to recover from each error. For HTTP `429`, read `Code` on `APIError`. If a proxy returned HTML, for example, the SDK returns `ProtocolError`. Both types expose `Metadata.HTTPStatus` and carry the original header in `Metadata.RetryAfter` when present. [Errors and retries](https://exchanger-api.iexexchanger.com/en/guides/reliability/errors.md)

Save the request body and idempotency key before the first attempt. After an ambiguous write failure, check the operation's state first. To retry order creation, pass the same JSON bytes and key to `client.Orders().CreateRaw(ctx, body, key)`. The SDK signs the new request with a fresh nonce. `Metadata.IdempotencyReplayed` indicates that the server returned a saved response. [Idempotency](https://exchanger-api.iexexchanger.com/en/guides/reliability/idempotency.md)

[Go SDK examples in GitHub](https://github.com/iexexchanger/exchanger-sdk-go/tree/420365c012c844220bfb1b01f93113ac0e4121b6/examples) · [Go SDK source code](https://github.com/iexexchanger/exchanger-sdk-go)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.