APIKeys - Python SDK
APIKeys method reference
(api_keys)
Overview
API key management endpoints
Available Operations
- list - List API keys
- create - Create a new API key
- update - Update an API key
- delete - Delete an API key
- get - Get a single API key
- get_current_key_metadata - Get current API key
list
List API keys
Example Usage
1 from openrouter import OpenRouter 2 import os 3 4 with 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
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
include_disabled | Optional[str] | ➖ | Whether to include disabled API keys in the response | false |
offset | Optional[str] | ➖ | Number of API keys to skip for pagination | 0 |
retries | Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
create
Create a new API key
Example Usage
1 from openrouter import OpenRouter 2 from openrouter.utils import parse_datetime 3 import os 4 5 with 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
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
name | str | ✔️ | Name for the new API key | My New API Key |
limit | OptionalNullable[float] | ➖ | Optional spending limit for the API key in USD | 50 |
limit_reset | OptionalNullable[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_limit | Optional[bool] | ➖ | Whether to include BYOK usage in the limit | true |
expires_at | date | ➖ | Optional ISO 8601 UTC timestamp when the API key should expire. Must be UTC, other timezones will be rejected | 2027-12-31T23:59:59Z |
retries | Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.BadRequestResponseError | 400 | application/json |
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
update
Update an API key
Example Usage
1 from openrouter import OpenRouter 2 import os 3 4 with 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
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
hash | str | ✔️ | The hash identifier of the API key to update | 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 |
name | Optional[str] | ➖ | New name for the API key | Updated API Key Name |
disabled | Optional[bool] | ➖ | Whether to disable the API key | false |
limit | OptionalNullable[float] | ➖ | New spending limit for the API key in USD | 75 |
limit_reset | OptionalNullable[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_limit | Optional[bool] | ➖ | Whether to include BYOK usage in the limit | true |
retries | Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.BadRequestResponseError | 400 | application/json |
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.NotFoundResponseError | 404 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
delete
Delete an API key
Example Usage
1 from openrouter import OpenRouter 2 import os 3 4 with 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
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
hash | str | ✔️ | The hash identifier of the API key to delete | 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 |
retries | Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.NotFoundResponseError | 404 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
get
Get a single API key
Example Usage
1 from openrouter import OpenRouter 2 import os 3 4 with 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
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
hash | str | ✔️ | The hash identifier of the API key to retrieve | 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 |
retries | Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.NotFoundResponseError | 404 | application/json |
| errors.TooManyRequestsResponseError | 429 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |
get_current_key_metadata
Get information on the API key associated with the current authentication session
Example Usage
1 from openrouter import OpenRouter 2 import os 3 4 with 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
| Parameter | Type | Required | Description |
|---|---|---|---|
retries | Optional[utils.RetryConfig] | ➖ | Configuration to override the default retry behavior of the client. |
Response
operations.GetCurrentKeyResponse
Errors
| Error Type | Status Code | Content Type |
|---|---|---|
| errors.UnauthorizedResponseError | 401 | application/json |
| errors.InternalServerResponseError | 500 | application/json |
| errors.OpenRouterDefaultError | 4XX, 5XX | */* |