For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
    • Overview
    • Usage for Agents
  • TypeScript SDK
    • Overview
  • Python SDK
    • Overview
  • Go SDK
      • Analytics
      • APIKeys
      • Byok
      • Chat
      • Credits
      • Embeddings
      • Endpoints
      • Generations
      • Guardrails
      • OAuth
      • Observability
      • Organization
      • Presets
      • Providers
      • Rerank
      • Beta.Responses
      • Transcriptions
      • Speech
      • VideoGeneration
      • Workspaces
  • DevTools
    • Overview
    • Migrating to @openrouter/agent
LogoLogo
ModelsChatRankingsDocs
On this page
  • Overview
  • Available Operations
  • ExchangeAuthCodeForAPIKey
  • Example Usage
  • Parameters
  • Response
  • Errors
  • CreateAuthCode
  • Example Usage
  • Parameters
  • Response
  • Errors
Go SDKAPI Reference

OAuth - Go SDK

OAuth method reference
Was this page helpful?
Previous

Observability - Go SDK

Observability method reference
Next
Built with

The Go SDK and docs are currently in beta. Report issues on GitHub.

Overview

OAuth authentication endpoints

Available Operations

  • ExchangeAuthCodeForAPIKey - Exchange authorization code for API key
  • CreateAuthCode - Create authorization code

ExchangeAuthCodeForAPIKey

Exchange an authorization code from the PKCE flow for a user-controlled API key

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/operations"
8 "github.com/OpenRouterTeam/go-sdk/optionalnullable"
9 "log"
10)
11
12func main() {
13 ctx := context.Background()
14
15 s := openrouter.New(
16 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
17 )
18
19 res, err := s.OAuth.ExchangeAuthCodeForAPIKey(ctx, operations.ExchangeAuthCodeForAPIKeyRequest{
20 Code: "auth_code_abc123def456",
21 CodeChallengeMethod: optionalnullable.From(openrouter.Pointer(operations.ExchangeAuthCodeForAPIKeyCodeChallengeMethodS256)),
22 CodeVerifier: openrouter.Pointer("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"),
23 })
24 if err != nil {
25 log.Fatal(err)
26 }
27 if res != nil {
28 // handle response
29 }
30}

Parameters

ParameterTypeRequiredDescription
ctxcontext.Context✔️The context to use for the request.
requestoperations.ExchangeAuthCodeForAPIKeyRequest✔️The request object to use for the request.
opts[]operations.Option➖The options for this request.

Response

*operations.ExchangeAuthCodeForAPIKeyResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.BadRequestResponseError400application/json
sdkerrors.ForbiddenResponseError403application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*

CreateAuthCode

Create an authorization code for the PKCE flow to generate a user-controlled API key

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "github.com/OpenRouterTeam/go-sdk/models/operations"
8 "log"
9)
10
11func main() {
12 ctx := context.Background()
13
14 s := openrouter.New(
15 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
16 )
17
18 res, err := s.OAuth.CreateAuthCode(ctx, operations.CreateAuthKeysCodeRequest{
19 CallbackURL: "https://myapp.com/auth/callback",
20 CodeChallenge: openrouter.Pointer("E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"),
21 CodeChallengeMethod: operations.CreateAuthKeysCodeCodeChallengeMethodS256.ToPointer(),
22 Limit: openrouter.Pointer[float64](100.0),
23 })
24 if err != nil {
25 log.Fatal(err)
26 }
27 if res != nil {
28 // handle response
29 }
30}

Parameters

ParameterTypeRequiredDescription
ctxcontext.Context✔️The context to use for the request.
requestoperations.CreateAuthKeysCodeRequest✔️The request object to use for the request.
opts[]operations.Option➖The options for this request.

Response

*operations.CreateAuthKeysCodeResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.BadRequestResponseError400application/json
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.ConflictResponseError409application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*