APIKeys - Go SDK

APIKeys method reference

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

Overview

API key management endpoints

Available Operations

List

List all API keys for the authenticated user. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.APIKeys.List(ctx, nil, nil)
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
includeDisabled*boolWhether to include disabled API keys in the responsefalse
offset*int64Number of API keys to skip for pagination0
opts[]operations.OptionThe options for this request.

Response

*operations.ListResponse, error

Errors

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

Create

Create a new API key for the authenticated user. Management key required.

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 "github.com/OpenRouterTeam/go-sdk/types"
10 "log"
11)
12
13func main() {
14 ctx := context.Background()
15
16 s := openrouter.New(
17 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
18 )
19
20 res, err := s.APIKeys.Create(ctx, operations.CreateKeysRequest{
21 Name: "My New API Key",
22 Limit: openrouter.Pointer[float64](50.0),
23 LimitReset: optionalnullable.From(openrouter.Pointer(operations.CreateKeysLimitResetMonthly)),
24 IncludeByokInLimit: openrouter.Pointer(true),
25 ExpiresAt: optionalnullable.From(openrouter.Pointer(types.MustNewTimeFromString("2027-12-31T23:59:59Z"))),
26 })
27 if err != nil {
28 log.Fatal(err)
29 }
30 if res != nil {
31 // handle response
32 }
33}

Parameters

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

Response

*operations.CreateKeysResponse, error

Errors

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

Update

Update an existing API key. Management key required.

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.APIKeys.Update(ctx, "f01d52606dc8f0a8303a7b5cc3fa07109c2e346cec7c0a16b40de462992ce943", operations.UpdateKeysRequestBody{
20 Name: openrouter.Pointer("Updated API Key Name"),
21 Disabled: openrouter.Pointer(false),
22 Limit: openrouter.Pointer[float64](75.0),
23 LimitReset: optionalnullable.From(openrouter.Pointer(operations.UpdateKeysLimitResetDaily)),
24 IncludeByokInLimit: openrouter.Pointer(true),
25 })
26 if err != nil {
27 log.Fatal(err)
28 }
29 if res != nil {
30 // handle response
31 }
32}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
hashstring✔️The hash identifier of the API key to updatef01d52606dc8f0a8303a7b5cc3fa07109c2e346cec7c0a16b40de462992ce943
requestBodyoperations.UpdateKeysRequestBody✔️N/A{"name": "Updated API Key Name","disabled": false,"limit": 75,"limit_reset": "daily","include_byok_in_limit": true}
opts[]operations.OptionThe options for this request.

Response

*operations.UpdateKeysResponse, error

Errors

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

Delete

Delete an existing API key. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.APIKeys.Delete(ctx, "f01d52606dc8f0a8303a7b5cc3fa07109c2e346cec7c0a16b40de462992ce943")
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
hashstring✔️The hash identifier of the API key to deletef01d52606dc8f0a8303a7b5cc3fa07109c2e346cec7c0a16b40de462992ce943
opts[]operations.OptionThe options for this request.

Response

*operations.DeleteKeysResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.NotFoundResponseError404application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*

Get

Get a single API key by hash. Management key required.

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.APIKeys.Get(ctx, "f01d52606dc8f0a8303a7b5cc3fa07109c2e346cec7c0a16b40de462992ce943")
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescriptionExample
ctxcontext.Context✔️The context to use for the request.
hashstring✔️The hash identifier of the API key to retrievef01d52606dc8f0a8303a7b5cc3fa07109c2e346cec7c0a16b40de462992ce943
opts[]operations.OptionThe options for this request.

Response

*operations.GetKeyResponse, error

Errors

Error TypeStatus CodeContent Type
sdkerrors.UnauthorizedResponseError401application/json
sdkerrors.NotFoundResponseError404application/json
sdkerrors.TooManyRequestsResponseError429application/json
sdkerrors.InternalServerResponseError500application/json
sdkerrors.APIError4XX, 5XX*/*

GetCurrentKeyMetadata

Get information on the API key associated with the current authentication session

Example Usage

1package main
2
3import(
4 "context"
5 "os"
6 openrouter "github.com/OpenRouterTeam/go-sdk"
7 "log"
8)
9
10func main() {
11 ctx := context.Background()
12
13 s := openrouter.New(
14 openrouter.WithSecurity(os.Getenv("OPENROUTER_API_KEY")),
15 )
16
17 res, err := s.APIKeys.GetCurrentKeyMetadata(ctx)
18 if err != nil {
19 log.Fatal(err)
20 }
21 if res != nil {
22 // handle response
23 }
24}

Parameters

ParameterTypeRequiredDescription
ctxcontext.Context✔️The context to use for the request.
opts[]operations.OptionThe options for this request.

Response

*operations.GetCurrentKeyResponse, error

Errors

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