APIKeys - Python SDK

APIKeys method reference

(api_keys)

Overview

API key management endpoints

Available Operations

list

List API keys

Example Usage

1from openrouter import OpenRouter
2import os
3
4with OpenRouter(
5 api_key=os.getenv("OPENROUTER_API_KEY", ""),
6) as open_router:
7
8 res = open_router.api_keys.list(include_disabled="false", offset="0")
9
10 # Handle response
11 print(res)

Parameters

ParameterTypeRequiredDescriptionExample
include_disabledOptional[str]Whether to include disabled API keys in the responsefalse
offsetOptional[str]Number of API keys to skip for pagination0
retriesOptional[utils.RetryConfig]Configuration to override the default retry behavior of the client.

Response

operations.ListResponse

Errors

Error TypeStatus CodeContent Type
errors.UnauthorizedResponseError401application/json
errors.TooManyRequestsResponseError429application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*

create

Create a new API key

Example Usage

1from openrouter import OpenRouter
2from openrouter.utils import parse_datetime
3import os
4
5with OpenRouter(
6 api_key=os.getenv("OPENROUTER_API_KEY", ""),
7) as open_router:
8
9 res = open_router.api_keys.create(name="My New API Key", limit=50, limit_reset="monthly", include_byok_in_limit=True, expires_at=parse_datetime("2027-12-31T23:59:59Z"))
10
11 # Handle response
12 print(res)

Parameters

ParameterTypeRequiredDescriptionExample
namestr✔️Name for the new API keyMy New API Key
limitOptionalNullable[float]Optional spending limit for the API key in USD50
limit_resetOptionalNullable[operations.CreateKeysLimitReset]Type of limit reset for the API key (daily, weekly, monthly, or null for no reset). Resets happen automatically at midnight UTC, and weeks are Monday through Sunday.monthly
include_byok_in_limitOptional[bool]Whether to include BYOK usage in the limittrue
expires_atdateOptional ISO 8601 UTC timestamp when the API key should expire. Must be UTC, other timezones will be rejected2027-12-31T23:59:59Z
retriesOptional[utils.RetryConfig]Configuration to override the default retry behavior of the client.

Response

operations.CreateKeysResponse

Errors

Error TypeStatus CodeContent Type
errors.BadRequestResponseError400application/json
errors.UnauthorizedResponseError401application/json
errors.TooManyRequestsResponseError429application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*

update

Update an API key

Example Usage

1from openrouter import OpenRouter
2import os
3
4with OpenRouter(
5 api_key=os.getenv("OPENROUTER_API_KEY", ""),
6) as open_router:
7
8 res = open_router.api_keys.update(hash="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", name="Updated API Key Name", disabled=False, limit=75, limit_reset="daily", include_byok_in_limit=True)
9
10 # Handle response
11 print(res)

Parameters

ParameterTypeRequiredDescriptionExample
hashstr✔️The hash identifier of the API key to updatesk-or-v1-0e6f44a47a05f1dad2ad7e88c4c1d6b77688157716fb1a5271146f7464951c96
nameOptional[str]New name for the API keyUpdated API Key Name
disabledOptional[bool]Whether to disable the API keyfalse
limitOptionalNullable[float]New spending limit for the API key in USD75
limit_resetOptionalNullable[operations.UpdateKeysLimitReset]New limit reset type for the API key (daily, weekly, monthly, or null for no reset). Resets happen automatically at midnight UTC, and weeks are Monday through Sunday.daily
include_byok_in_limitOptional[bool]Whether to include BYOK usage in the limittrue
retriesOptional[utils.RetryConfig]Configuration to override the default retry behavior of the client.

Response

operations.UpdateKeysResponse

Errors

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

delete

Delete an API key

Example Usage

1from openrouter import OpenRouter
2import os
3
4with OpenRouter(
5 api_key=os.getenv("OPENROUTER_API_KEY", ""),
6) as open_router:
7
8 res = open_router.api_keys.delete(hash="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")
9
10 # Handle response
11 print(res)

Parameters

ParameterTypeRequiredDescriptionExample
hashstr✔️The hash identifier of the API key to deletesk-or-v1-0e6f44a47a05f1dad2ad7e88c4c1d6b77688157716fb1a5271146f7464951c96
retriesOptional[utils.RetryConfig]Configuration to override the default retry behavior of the client.

Response

operations.DeleteKeysResponse

Errors

Error TypeStatus CodeContent Type
errors.UnauthorizedResponseError401application/json
errors.NotFoundResponseError404application/json
errors.TooManyRequestsResponseError429application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*

get

Get a single API key

Example Usage

1from openrouter import OpenRouter
2import os
3
4with OpenRouter(
5 api_key=os.getenv("OPENROUTER_API_KEY", ""),
6) as open_router:
7
8 res = open_router.api_keys.get(hash="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")
9
10 # Handle response
11 print(res)

Parameters

ParameterTypeRequiredDescriptionExample
hashstr✔️The hash identifier of the API key to retrievesk-or-v1-0e6f44a47a05f1dad2ad7e88c4c1d6b77688157716fb1a5271146f7464951c96
retriesOptional[utils.RetryConfig]Configuration to override the default retry behavior of the client.

Response

operations.GetKeyResponse

Errors

Error TypeStatus CodeContent Type
errors.UnauthorizedResponseError401application/json
errors.NotFoundResponseError404application/json
errors.TooManyRequestsResponseError429application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*

get_current_key_metadata

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

Example Usage

1from openrouter import OpenRouter
2import os
3
4with OpenRouter(
5 api_key=os.getenv("OPENROUTER_API_KEY", ""),
6) as open_router:
7
8 res = open_router.api_keys.get_current_key_metadata()
9
10 # Handle response
11 print(res)

Parameters

ParameterTypeRequiredDescription
retriesOptional[utils.RetryConfig]Configuration to override the default retry behavior of the client.

Response

operations.GetCurrentKeyResponse

Errors

Error TypeStatus CodeContent Type
errors.UnauthorizedResponseError401application/json
errors.InternalServerResponseError500application/json
errors.OpenRouterDefaultError4XX, 5XX*/*