# Setup

Add `go.ideal-postcodes.co.uk/sdk` to your module, then create a client with your API key. The SDK needs Go 1.26 or later and has no other dependencies.

## Install

```bash
go get go.ideal-postcodes.co.uk/sdk
```

## Create a client

Pass your API key to `NewClient()`. The package name is `idealpostcodes`, so import it under that name.

client.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.Postcodes(context.Background(), "BR8 7RE", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(len(res.Result), "addresses")
}
```

A `Client` is safe for concurrent use. Create one and share it.

## Management Key

Key details, usage and logs, licensees and configurations need your Management Key, which begins `uk_`. Pass it with `WithManagementKey()`. It grants control of your account, so keep it on your server and out of version control.

management.gogo

```go
package main

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

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test",
		idealpostcodes.WithManagementKey(os.Getenv("IDPC_MANAGEMENT_KEY")))
	details, err := client.KeyDetails(context.Background(), "ak_test")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(details.Result.LookupsRemaining)
}
```

A client without a Management Key returns `idealpostcodes.ErrMissingManagementKey` from those methods, before it sends a request.

## Client options

`NewClient()` takes options after the key.

| Option                         | Purpose                                                                                                                          |
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| `WithManagementKey(string)`    | The Management Key for account operations                                                                                        |
| `WithHTTPClient(*http.Client)` | The HTTP client to send requests through. The default times out after 30 seconds                                                 |
| `WithBaseURL(string)`          | Defaults to `https://api.ideal-postcodes.co.uk/v1`                                                                               |
| `WithUserAgent(string)`        | A product sent ahead of the SDK and Go versions in `User-Agent`, for example `my-app/1.0 ideal-postcodes-go-sdk/0.1.0 go/1.27.4` |

## Request timeout

Set a deadline on the context for one call, or a `Timeout` on the HTTP client for every call. A request that runs past either returns an error that `errors.Is` matches to `context.DeadlineExceeded`, as described in [error handling](/docs/sdks/go/errors.md).

timeout.gogo

```go
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"time"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test",
		idealpostcodes.WithHTTPClient(&http.Client{Timeout: 10 * time.Second}))

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	res, err := client.Postcodes(ctx, "BR8 7RE", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(len(res.Result), "addresses")
}
```

## Credentials

The API key goes in the `Authorization` header of every address, place, email and phone request. The Management Key goes in the same header of every key, licensee and configuration request, and nowhere else. Public operations such as `KeyAvailability()` send no credentials.

To bill requests to a [licensee](/docs/sdks/go/licensees.md), create a client with the licensee's key, which begins `sl_`, in place of your API key.
