> ## Documentation Index
> Fetch the complete documentation index at: https://docs.near.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Go

> Use the Go SDK for verified Chat Completions, Responses, Messages, and System One decisions.

## Install

Requires Go **1.26.10 or later**. Install the published SDK in your application:

```bash theme={"dark"}
go mod init example.com/my-app # If your project does not have a go.mod yet.
go get github.com/nearai/inference-sdk/go
export NEARAI_API_KEY="your-api-key"
```

## Examples

Choose the tab matching your model's API. Save the example as `main.go` and run it with `go run .`.

> [tab] Chat Completions
>
> Call a supported NEAR TEE model with E2EE enabled, then verify the completion.
>
> ```go theme={"dark"}
> package main
>
> import (
> 	"context"
> 	"encoding/json"
> 	"fmt"
> 	"log"
> 	"os"
> 	"time"
>
> 	nearai "github.com/nearai/inference-sdk/go"
> )
>
> func main() {
> 	if err := run(); err != nil {
> 		log.Fatal(err)
> 	}
> }
>
> func run() error {
> 	apiKey := os.Getenv("NEARAI_API_KEY")
> 	if apiKey == "" {
> 		return fmt.Errorf("set NEARAI_API_KEY")
> 	}
> 	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
> 	defer cancel()
>
> 	client, err := nearai.NewInferenceClient(nearai.InferenceOptions{
> 		ClientOptions: nearai.ClientOptions{APIKey: apiKey},
> 		E2EE:          true,
> 	})
> 	if err != nil {
> 		return err
> 	}
> 	defer client.Close()
>
> 	body, err := client.CreateChatCompletion(ctx, map[string]any{
> 		"model":    "z-ai/glm-5.3-flash",
> 		"messages": []map[string]string{{"role": "user", "content": "Hello!"}},
> 	})
> 	if err != nil {
> 		return err
> 	}
> 	var completion struct {
> 		ID string `json:"id"`
> 	}
> 	if err := json.Unmarshal(body, &completion); err != nil {
> 		return err
> 	}
> 	if completion.ID == "" {
> 		return fmt.Errorf("completion has no ID")
> 	}
>
> 	verified, err := client.VerifyResponse(ctx, completion.ID)
> 	if err != nil {
> 		return err
> 	}
> 	fmt.Println(verified.SignatureKind)
> 	fmt.Println(string(body))
> 	return nil
> }
> ```

> [tab] Responses
>
> Use a Responses model with `store: false` and E2EE disabled. See [Stateless Responses](https://docs.near.ai/cloud/guides/stateless-responses.md) for conversation history and tool calls.
>
> ```go theme={"dark"}
> package main
>
> import (
>     "context"
>     "encoding/json"
>     "fmt"
>     "log"
>     "os"
>     "time"
>
>     nearai "github.com/nearai/inference-sdk/go"
> )
>
> func main() {
>     if err := run(); err != nil {
>         log.Fatal(err)
>     }
> }
>
> func run() error {
>     apiKey := os.Getenv("NEARAI_API_KEY")
>     if apiKey == "" {
>         return fmt.Errorf("set NEARAI_API_KEY first")
>     }
>     ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
>     defer cancel()
>
>     client, err := nearai.NewInferenceClient(nearai.InferenceOptions{
>         ClientOptions: nearai.ClientOptions{
>             APIKey: apiKey,
>             BaseURL: "https://cloud-api.near.ai/v1",
>         },
>         E2EE: false,
>     })
>     if err != nil {
>         return err
>     }
>     defer client.Close()
>
>     raw, err := client.CreateResponse(ctx, map[string]any{
>         "model": "openai/gpt-6-astra",
>         "input": "Hello!",
>         "store": false,
>     })
>     if err != nil {
>         return err
>     }
>     var response struct {
>         ID string `json:"id"`
>     }
>     if err := json.Unmarshal(raw, &response); err != nil {
>         return err
>     }
>     if response.ID == "" {
>         return fmt.Errorf("response has no ID")
>     }
>
>     // Verify before displaying the answer.
>     verified, err := client.VerifyResponse(ctx, response.ID)
>     if err != nil {
>         return err
>     }
>     fmt.Println(string(raw))
>     fmt.Println("Verified signer:", verified.SignatureKind)
>     return nil
> }
> ```

> [tab] Messages
>
> Call an Anthropic model through the Messages API with E2EE disabled.
>
> ```go theme={"dark"}
> package main
>
> import (
>     "context"
>     "encoding/json"
>     "fmt"
>     "log"
>     "os"
>     "time"
>
>     nearai "github.com/nearai/inference-sdk/go"
> )
>
> func main() {
>     if err := run(); err != nil {
>         log.Fatal(err)
>     }
> }
>
> func run() error {
>     apiKey := os.Getenv("NEARAI_API_KEY")
>     if apiKey == "" {
>         return fmt.Errorf("set NEARAI_API_KEY first")
>     }
>     ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
>     defer cancel()
>     client, err := nearai.NewInferenceClient(nearai.InferenceOptions{
>         ClientOptions: nearai.ClientOptions{
>             APIKey: apiKey,
>             BaseURL: "https://cloud-api.near.ai/v1",
>         },
>         E2EE: false,
>     })
>     if err != nil {
>         return err
>     }
>     defer client.Close()
>
>     raw, err := client.CreateMessage(ctx, map[string]any{
>         "model": "anthropic/claude-opus-4-6",
>         "max_tokens": 128,
>         "messages": []map[string]string{{"role": "user", "content": "Hello!"}},
>     })
>     if err != nil {
>         return err
>     }
>     var response struct {
>         ID string
>         Content []struct { Type, Text string }
>     }
>     if err := json.Unmarshal(raw, &response); err != nil {
>         return err
>     }
>     if response.ID == "" {
>         return fmt.Errorf("message has no ID")
>     }
>
>     // Verify before displaying the answer.
>     verified, err := client.VerifyResponse(ctx, response.ID)
>     if err != nil {
>         return err
>     }
>     for _, block := range response.Content {
>         if block.Type == "text" {
>             fmt.Println(block.Text)
>         }
>     }
>     fmt.Println("Verified signer:", verified.SignatureKind)
>     return nil
> }
> ```

> [tab] System One
>
> Ask Jev a structured question with E2EE and OHTTP disabled.
>
> ```go theme={"dark"}
> package main
>
> import (
>     "context"
>     "encoding/json"
>     "fmt"
>     "log"
>     "os"
>     "time"
>
>     nearai "github.com/nearai/inference-sdk/go"
> )
>
> func main() {
>     if err := run(); err != nil {
>         log.Fatal(err)
>     }
> }
>
> func run() error {
>     apiKey := os.Getenv("NEARAI_API_KEY")
>     if apiKey == "" {
>         return fmt.Errorf("set NEARAI_API_KEY first")
>     }
>     ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
>     defer cancel()
>     client, err := nearai.NewInferenceClient(nearai.InferenceOptions{
>         ClientOptions: nearai.ClientOptions{
>             APIKey: apiKey,
>             BaseURL: "https://cloud-api.near.ai/v1",
>         },
>         E2EE: false,
>         OHTTP: false,
>     })
>     if err != nil {
>         return err
>     }
>     defer client.Close()
>
>     result, err := client.CreateSystemOne(ctx, nearai.SystemOneRequest{
>         Model: "typesafe/jev-1.13",
>         State: map[string]string{"message": "I was charged twice. Please help."},
>         Questions: map[string]nearai.SystemOneQuestion{
>             "billing": {Type: "noul", Instructions: "Is this about billing?"},
>         },
>     })
>     if err != nil {
>         return err
>     }
>     // DecisionID is captured from X-Generation-Id.
>     verified, err := client.VerifyResponse(ctx, result.DecisionID)
>     if err != nil {
>         return err
>     }
>     answers, err := json.MarshalIndent(result.Data.Answers, "", "  ")
>     if err != nil {
>         return err
>     }
>     fmt.Println(string(answers))
>     fmt.Println("Verified signer:", verified.SignatureKind)
>     return nil
> }
> ```

## E2EE

Gateway E2EE is disabled by default. Set `E2EE: true` to encrypt supported Chat fields to an attested model key and decrypt the response automatically.

E2EE requires a NEAR vLLM model with attestation enabled. Other providers use Gateway-only verification; leave E2EE disabled for those models.

## Verification

Before sending Chat, the client verifies the Gateway and every returned model report for supported models. Attested Gateway TLS pinning is enabled by default; normal HTTPS certificate validation remains enabled. Failed required checks stop the request.

Response-signature verification is explicit: call `client.VerifyResponse(ctx, completionID)` after fully consuming the response. The client retains exact request and response bytes automatically, including encrypted wire bytes when E2EE is enabled.

See [Verification](https://docs.near.ai/cloud/inference-sdk#verification) for receipt IDs and signature kinds.

Response records expire 60 minutes after the full response is read, by default. Verify each response before its record expires or the client closes.

To check a deployment without sending a prompt, call `client.Verify(ctx, model)`. For custom policies and cache settings, see the [Go verification guide](https://github.com/nearai/inference-sdk/blob/main/go/docs/verification-guide.md).

## OHTTP

Set `OHTTP: true` in `InferenceOptions` to encapsulate the Chat HTTP exchange to the attested Gateway. OHTTP is disabled by default and requires Ed25519, the default signing algorithm. Configure E2EE independently. Missing or invalid Gateway OHTTP evidence blocks Chat.

> [!NOTE]
>
> OHTTP supports Chat Completions, Responses, and Messages through the Gateway; System One does not support OHTTP. Attestation and signature requests remain regular HTTPS. The endpoint must expose `/ohttp` at the same origin. Outer authorization headers and the client's network address are not hidden by OHTTP.

## Examples and reference

* [Go verification guide](https://github.com/nearai/inference-sdk/blob/main/go/docs/verification-guide.md): policies, encryption, streaming, TLS binding, and errors.
* [Go API reference](https://github.com/nearai/inference-sdk/blob/main/go/docs/api-reference.md): client options, verification functions, defaults, and result fields.
* [Runnable Go examples](https://github.com/nearai/inference-sdk/tree/main/examples/example-go): Gateway and direct clients, standalone verification, and streaming.
* OpenAI SDK integration: [Chat Completions](https://github.com/nearai/inference-sdk/blob/main/examples/example-go/gateway/chat-completions-with-openai-sdk/main.go) and [Responses](https://github.com/nearai/inference-sdk/blob/main/examples/example-go/gateway/responses-with-openai-sdk/main.go).
* [Anthropic SDK integration](https://github.com/nearai/inference-sdk/blob/main/examples/example-go/gateway/messages-with-anthropic-sdk/main.go).

For experimental direct endpoints, see the [direct-client guide](https://docs.near.ai/cloud/inference-sdk/direct.md).


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