OAuth - Go SDK

OAuth method reference

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

Overview

OAuth authentication endpoints

Available Operations

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 CodeVerifier: openrouter.Pointer("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"),
22 CodeChallengeMethod: optionalnullable.From(openrouter.Pointer(operations.ExchangeAuthCodeForAPIKeyCodeChallengeMethodS256)),
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.OptionThe 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.OptionThe 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*/*