From 64159ffbde66dbc58d05c867654436f339284dbb Mon Sep 17 00:00:00 2001 From: dpiet-oai <326640584+dpiet-oai@users.noreply.github.com> Date: Fri, 4 Dec 2026 17:54:47 +0000 Subject: [PATCH] feat(api): add project API key status filters Castiron-Internal-PR: https://github.com/openai/openai-python-internal/pull/473 Castiron-Source-SHA: aee1492df152d1e0eb6e9734030508d2c3ba5b17 Castiron-Public-Base-SHA: 8f651c7cf245d34236dd2c4a8f734f5a0bb46add --- .castiron.stats.yml | 10 +- api.md | 2 +- api_reference/openapi.transformed.yml | 153 ++++++++++++++++-- scripts/castiron/generated-files.json | 16 +- .../admin/organization/projects/api_keys.py | 90 +++++++++-- .../admin/organization/projects/__init__.py | 1 + .../projects/api_key_list_params.py | 14 +- .../projects/api_key_retrieve_params.py | 19 +++ .../organization/projects/project_api_key.py | 8 + .../organization/projects/test_api_keys.py | 20 +++ tests/test_project_api_key_status.py | 72 +++++++++ 11 files changed, 367 insertions(+), 38 deletions(-) create mode 100644 src/openai/types/admin/organization/projects/api_key_retrieve_params.py create mode 100644 tests/test_project_api_key_status.py diff --git a/.castiron.stats.yml b/.castiron.stats.yml index 06d9408f34..871613bd51 100644 --- a/.castiron.stats.yml +++ b/.castiron.stats.yml @@ -1,7 +1,7 @@ schema_version: 1 -generation_id: 9126dcfe-8d90-474b-94c3-e8385bd3789d -openapi_spec_hash: 5e1804457f3d482bd25e99e86f1988d6 -openapi_transformed_spec_hash: 79577ce15c57e0d07a472e594c7bd768 +generation_id: d5a836a1-a288-4f85-82fe-0acd3029dd32 +openapi_spec_hash: 5aa98e120f6bf7b33a6b37f70ab7c0cc +openapi_transformed_spec_hash: 713f7faec0360a06874a5c192506b8c9 config_hash: fb6bc01fab5ad1159ebbfcd9580b679d -codegen_sha: 544266af1a80139c2a0cafbad6e7192ffb79a6a5 -codegen_hash: ad5a89bfdc0b7ab49927c66b76475a9fa9a3e89a1147972d92a2a97a0b8a6270 +codegen_sha: 5ab04658b2ade60c1e2b9cbbd28be11e8a9fcd2d +codegen_hash: 16e60637734eedab73950020a48df8f5c41b7ee0cd1d89a2f848e88e3fb39739 diff --git a/api.md b/api.md index 91e22f3420..37737fde9d 100644 --- a/api.md +++ b/api.md @@ -1750,7 +1750,7 @@ from openai.types.admin.organization.projects import ProjectAPIKey, APIKeyDelete Methods: -- client.admin.organization.projects.api_keys.retrieve(api_key_id, \*, project_id) -> ProjectAPIKey +- client.admin.organization.projects.api_keys.retrieve(api_key_id, \*, project_id, \*\*params) -> ProjectAPIKey - client.admin.organization.projects.api_keys.update(api_key_id, \*, project_id, \*\*params) -> ProjectAPIKey - client.admin.organization.projects.api_keys.list(project_id, \*\*params) -> SyncConversationCursorPage[ProjectAPIKey] - client.admin.organization.projects.api_keys.delete(api_key_id, \*, project_id) -> APIKeyDeleteResponse diff --git a/api_reference/openapi.transformed.yml b/api_reference/openapi.transformed.yml index a8473b082f..8fc56ad2ac 100644 --- a/api_reference/openapi.transformed.yml +++ b/api_reference/openapi.transformed.yml @@ -12940,7 +12940,17 @@ paths: security: - AdminApiKeyAuth: [] summary: List project API keys - description: Returns a list of API keys in the project. + description: | + Returns a list of API keys in the project. Use `status` to list active, + inactive, or all retained keys. Inactive keys include expired, disabled, + and revoked keys. Omitting `status` preserves the existing results and + ordering, which can include expired keys. + + Status-filtered lists may take time to reflect changes, including revocations. + Keep filters unchanged between pages; each page is a separate read. To check + a known key's current retained metadata, use the retrieve endpoint with + `include_disabled=true`. Keys whose owner or redacted value is no longer + retained are omitted. operationId: list-project-api-keys tags: - Projects @@ -12969,7 +12979,7 @@ paths: - name: owner_project_access in: query description: | - Filter API keys by whether the owner currently has effective access to the project. Use `active` for owners with access, `inactive` for owners without access, or `any` for all enabled project API keys. If omitted, the endpoint applies its existing membership-based visibility rules, which may exclude some enabled keys. + Filter API keys by whether the owner currently has effective access to the project. Use `active` for owners with access, `inactive` for owners without access, or `any` to include retained keys whose owners have lost access. This filter is independent of the key's `status`. If omitted, the endpoint applies its existing membership-based visibility rules. required: false schema: type: string @@ -12977,6 +12987,13 @@ paths: - active - inactive - any + - name: status + in: query + description: | + Filter by the credential's status, independently of its owner's project access. Use `active` for enabled, unexpired keys, `inactive` for expired, disabled, or revoked keys, or `all` for both. If omitted, existing results and ordering are preserved, which can include expired keys. + required: false + schema: + $ref: '#/components/schemas/ProjectApiKeyStatusFilter' responses: '200': description: Project API keys listed successfully. @@ -12985,7 +13002,8 @@ paths: schema: $ref: '#/components/schemas/ProjectApiKeyListResponse' '400': - description: The listing parameters are invalid or the project is archived. + description: | + The listing parameters are invalid, the project is archived, or `status=inactive` or `status=all` was requested when history listing is not available for the organization. content: application/json: schema: @@ -13011,9 +13029,10 @@ paths: x-oaiMeta: group: administration examples: + - title: List project keys request: curl: | - curl https://api.openai.com/v1/organization/projects/proj_abc/api_keys?after=key_abc&limit=20&owner_project_access=any \ + curl "https://api.openai.com/v1/organization/projects/proj_abc/api_keys?limit=20&owner_project_access=any" \ -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ -H "Content-Type: application/json" response: | @@ -13025,6 +13044,9 @@ paths: "redacted_value": "sk-abc...def", "name": "My API Key", "created_at": 1711471533, + "status": "active", + "deleted_at": null, + "expires_at": null, "last_used_at": 1711471534, "id": "key_abc", "owner_project_access": "active", @@ -13041,7 +13063,44 @@ paths: } ], "first_id": "key_abc", - "last_id": "key_xyz", + "last_id": "key_abc", + "has_more": false + } + - title: List inactive keys + request: + curl: | + curl "https://api.openai.com/v1/organization/projects/proj_abc/api_keys?status=inactive&limit=20&owner_project_access=any" \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: | + { + "object": "list", + "data": [ + { + "object": "organization.project.api_key", + "redacted_value": "sk-abc...def", + "name": "Revoked API Key", + "created_at": 1711471533, + "status": "inactive", + "deleted_at": 1711557933, + "expires_at": null, + "last_used_at": 1711471534, + "id": "key_revoked", + "owner_project_access": "active", + "owner": { + "type": "user", + "user": { + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "created_at": 1711471533 + } + } + } + ], + "first_id": "key_revoked", + "last_id": "key_revoked", "has_more": false } /organization/projects/{project_id}/api_keys/{api_key_id}: @@ -13049,7 +13108,14 @@ paths: security: - AdminApiKeyAuth: [] summary: Retrieve project API key - description: Retrieves an API key in the project. + description: | + Retrieves an API key's current retained metadata in the project. + + Set `include_disabled=true` to retrieve disabled or revoked keys, or active + keys whose owners have lost project access. Revoked keys return + `status: "inactive"`. Without this option, existing not-found responses are + preserved. Keys whose owner or redacted value is no longer retained return + `404` even when `include_disabled=true`. operationId: retrieve-project-api-key tags: - Projects @@ -13066,6 +13132,14 @@ paths: required: true schema: type: string + - name: include_disabled + in: query + description: | + Allow retrieval of disabled or revoked keys with retained owner and redacted metadata. Also returns active keys whose owners have lost project access; their `owner_project_access` is `inactive`. Defaults to `false`, preserving existing not-found responses. + required: false + schema: + type: boolean + default: false responses: '200': description: Project API key retrieved successfully. @@ -13074,7 +13148,7 @@ paths: schema: $ref: '#/components/schemas/ProjectApiKey' '400': - description: The project is archived. + description: The query parameters are invalid or the project is archived. content: application/json: schema: @@ -13086,7 +13160,8 @@ paths: schema: $ref: '#/components/schemas/PermissionErrorResponse' '404': - description: The project or API key was not found. + description: | + The project or API key was not found, the key is not visible with the requested options, or its owner or redacted value is no longer retained. content: application/json: schema: @@ -13100,9 +13175,10 @@ paths: x-oaiMeta: group: administration examples: + - title: Retrieve a key request: curl: | - curl https://api.openai.com/v1/organization/projects/proj_abc/api_keys/key_abc \ + curl "https://api.openai.com/v1/organization/projects/proj_abc/api_keys/key_abc" \ -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ -H "Content-Type: application/json" response: | @@ -13111,6 +13187,9 @@ paths: "redacted_value": "sk-abc...def", "name": "My API Key", "created_at": 1711471533, + "status": "active", + "deleted_at": null, + "expires_at": null, "last_used_at": 1711471534, "id": "key_abc", "owner_project_access": "active", @@ -13125,6 +13204,35 @@ paths: } } } + - title: Retrieve a revoked key + request: + curl: | + curl "https://api.openai.com/v1/organization/projects/proj_abc/api_keys/key_revoked?include_disabled=true" \ + -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \ + -H "Content-Type: application/json" + response: | + { + "object": "organization.project.api_key", + "redacted_value": "sk-abc...def", + "name": "Revoked API Key", + "created_at": 1711471533, + "status": "inactive", + "deleted_at": 1711557933, + "expires_at": null, + "last_used_at": 1711471534, + "id": "key_revoked", + "owner_project_access": "active", + "owner": { + "type": "user", + "user": { + "id": "user_abc", + "name": "First Last", + "email": "user@example.com", + "role": "owner", + "created_at": 1711471533 + } + } + } delete: security: - AdminApiKeyAuth: [] @@ -13291,6 +13399,8 @@ paths: "redacted_value": "sk-abc...def", "name": "My API Key", "created_at": 1711471533, + "status": "active", + "deleted_at": null, "expires_at": 1711557933, "last_used_at": null, "id": "key_abc", @@ -55773,6 +55883,10 @@ components: $ref: '#/components/schemas/ProjectApiKeyOwnerServiceAccount' spend_limit: $ref: '#/components/schemas/spend_limit' + status: + $ref: '#/components/schemas/ProjectApiKeyStatus' + deleted_at: + $ref: '#/components/schemas/deleted_at' required: - object - redacted_value @@ -55790,7 +55904,9 @@ components: "redacted_value": "sk-abc...def", "name": "My API Key", "created_at": 1711471533, - "expires_at": 1714063533, + "status": "active", + "deleted_at": null, + "expires_at": null, "last_used_at": 1711471534, "id": "key_abc", "owner_project_access": "active", @@ -115429,6 +115545,23 @@ components: - data - has_more - next_page + ProjectApiKeyStatusFilter: + type: string + enum: + - active + - inactive + - all + ProjectApiKeyStatus: + type: string + enum: + - active + - inactive + deleted_at: + anyOf: + - type: integer + format: unixtime + description: The Unix timestamp (in seconds) when the key was revoked, or null if unavailable. + - type: 'null' CostsResultWithDimensions: type: object description: The aggregated costs details of the specific time bucket. diff --git a/scripts/castiron/generated-files.json b/scripts/castiron/generated-files.json index 23610b0b1d..04284b198e 100644 --- a/scripts/castiron/generated-files.json +++ b/scripts/castiron/generated-files.json @@ -3,7 +3,7 @@ "files": { "api.md": { "mode": "100644", - "sha256": "4afd58ab9dca50c1a2ca5543cc8d53cbd6268b72be57264f2b838eb3440d5792" + "sha256": "ab3b66827ab4be37da511c4d49c013b50dc5c9f5bb37cf24d3cfc60e103999d9" }, "src/openai/__init__.py": { "mode": "100644", @@ -111,7 +111,7 @@ }, "src/openai/resources/admin/organization/projects/api_keys.py": { "mode": "100644", - "sha256": "a7604a47d36f00779ce3490301f28d180c59784febaa162d9468c22446ef8753" + "sha256": "e23de7841ff07e4fe4b3afb5b5a726c3be2847d2a5ae88bcfb1085b15ab71e5c" }, "src/openai/resources/admin/organization/projects/certificates.py": { "mode": "100644", @@ -1083,7 +1083,7 @@ }, "src/openai/types/admin/organization/projects/__init__.py": { "mode": "100644", - "sha256": "91e0832a521c9eac829ad94b17619bbe1038e4920a93f1deca94947b0093c410" + "sha256": "4c5ddc36c3e8087b8a4879cd6c2c62a13f51ffdf084b2f0be034377529791f44" }, "src/openai/types/admin/organization/projects/api_key_delete_response.py": { "mode": "100644", @@ -1091,7 +1091,11 @@ }, "src/openai/types/admin/organization/projects/api_key_list_params.py": { "mode": "100644", - "sha256": "e4df85365ee0be5fba74c61c6f475a545d5e3305274bd07adcbee49c02166a90" + "sha256": "05b7f8acf045e711fa841e4e6b8b04c67a3ec869909710e3da0ac43724742944" + }, + "src/openai/types/admin/organization/projects/api_key_retrieve_params.py": { + "mode": "100644", + "sha256": "82192fedb4d8bc46d487021c8dcae444e8e5c952df155fd8849bf9921cb6e06d" }, "src/openai/types/admin/organization/projects/api_key_update_params.py": { "mode": "100644", @@ -1199,7 +1203,7 @@ }, "src/openai/types/admin/organization/projects/project_api_key.py": { "mode": "100644", - "sha256": "1c69e69513f420f3ee516c991135e3d3934c235c5b90a09c8b3b72672743a7c8" + "sha256": "50ec6ae9403e95c9b42d6d87d4b16ef964d865545b49dbaa0f40ade0b2f4568e" }, "src/openai/types/admin/organization/projects/project_data_retention.py": { "mode": "100644", @@ -7683,7 +7687,7 @@ }, "tests/api_resources/admin/organization/projects/test_api_keys.py": { "mode": "100644", - "sha256": "3bf3488d59ae5193b997c9308ca9faf7008ac9f26b9e40212a9066380cb701d5" + "sha256": "0f330ff6ac3a09bb3ab34c4e03f0450db63df4654779a315314bb6315a7c1a55" }, "tests/api_resources/admin/organization/projects/test_certificates.py": { "mode": "100644", diff --git a/src/openai/resources/admin/organization/projects/api_keys.py b/src/openai/resources/admin/organization/projects/api_keys.py index 920a0de199..58bd4ceb75 100644 --- a/src/openai/resources/admin/organization/projects/api_keys.py +++ b/src/openai/resources/admin/organization/projects/api_keys.py @@ -15,7 +15,7 @@ from ....._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper from .....pagination import SyncConversationCursorPage, AsyncConversationCursorPage from ....._base_client import AsyncPaginator, make_request_options -from .....types.admin.organization.projects import api_key_list_params, api_key_update_params +from .....types.admin.organization.projects import api_key_list_params, api_key_update_params, api_key_retrieve_params from .....types.admin.organization.projects.project_api_key import ProjectAPIKey from .....types.admin.organization.projects.api_key_delete_response import APIKeyDeleteResponse @@ -47,6 +47,7 @@ def retrieve( api_key_id: str, *, project_id: str, + include_disabled: bool | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, @@ -55,9 +56,20 @@ def retrieve( timeout: float | httpx2.Timeout | None | NotGiven = not_given, ) -> ProjectAPIKey: """ - Retrieves an API key in the project. + Retrieves an API key's current retained metadata in the project. + + Set `include_disabled=true` to retrieve disabled or revoked keys, or active + keys whose owners have lost project access. Revoked keys return + `status: "inactive"`. Without this option, existing not-found responses are + preserved. Keys whose owner or redacted value is no longer retained return + `404` even when `include_disabled=true`. Args: + include_disabled: Allow retrieval of disabled or revoked keys with retained owner and redacted + metadata. Also returns active keys whose owners have lost project access; their + `owner_project_access` is `inactive`. Defaults to `false`, preserving existing + not-found responses. + extra_headers: Send extra headers extra_query: Add additional query parameters to the request @@ -81,6 +93,9 @@ def retrieve( extra_query=extra_query, extra_body=extra_body, timeout=timeout, + query=maybe_transform( + {"include_disabled": include_disabled}, api_key_retrieve_params.APIKeyRetrieveParams + ), security={"admin_api_key_auth": True}, ), cast_to=ProjectAPIKey, @@ -179,6 +194,7 @@ def list( after: str | Omit = omit, limit: int | Omit = omit, owner_project_access: Literal["active", "inactive", "any"] | Omit = omit, + status: Literal["active", "inactive", "all"] | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, @@ -186,8 +202,18 @@ def list( extra_body: Body | None = None, timeout: float | httpx2.Timeout | None | NotGiven = not_given, ) -> SyncConversationCursorPage[ProjectAPIKey]: - """ - Returns a list of API keys in the project. + """Returns a list of API keys in the project. + + Use `status` to list active, + inactive, or all retained keys. Inactive keys include expired, disabled, and + revoked keys. Omitting `status` preserves the existing results and ordering, + which can include expired keys. + + Status-filtered lists may take time to reflect changes, including revocations. + Keep filters unchanged between pages; each page is a separate read. To check a + known key's current retained metadata, use the retrieve endpoint with + `include_disabled=true`. Keys whose owner or redacted value is no longer + retained are omitted. Args: after: A cursor for use in pagination. `after` is an object ID that defines your place @@ -200,9 +226,14 @@ def list( owner_project_access: Filter API keys by whether the owner currently has effective access to the project. Use `active` for owners with access, `inactive` for owners without - access, or `any` for all enabled project API keys. If omitted, the endpoint - applies its existing membership-based visibility rules, which may exclude some - enabled keys. + access, or `any` to include retained keys whose owners have lost access. This + filter is independent of the key's `status`. If omitted, the endpoint applies + its existing membership-based visibility rules. + + status: Filter by the credential's status, independently of its owner's project access. + Use `active` for enabled, unexpired keys, `inactive` for expired, disabled, or + revoked keys, or `all` for both. If omitted, existing results and ordering are + preserved, which can include expired keys. extra_headers: Send extra headers @@ -227,6 +258,7 @@ def list( "after": after, "limit": limit, "owner_project_access": owner_project_access, + "status": status, }, api_key_list_params.APIKeyListParams, ), @@ -310,6 +342,7 @@ async def retrieve( api_key_id: str, *, project_id: str, + include_disabled: bool | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, @@ -318,9 +351,20 @@ async def retrieve( timeout: float | httpx2.Timeout | None | NotGiven = not_given, ) -> ProjectAPIKey: """ - Retrieves an API key in the project. + Retrieves an API key's current retained metadata in the project. + + Set `include_disabled=true` to retrieve disabled or revoked keys, or active + keys whose owners have lost project access. Revoked keys return + `status: "inactive"`. Without this option, existing not-found responses are + preserved. Keys whose owner or redacted value is no longer retained return + `404` even when `include_disabled=true`. Args: + include_disabled: Allow retrieval of disabled or revoked keys with retained owner and redacted + metadata. Also returns active keys whose owners have lost project access; their + `owner_project_access` is `inactive`. Defaults to `false`, preserving existing + not-found responses. + extra_headers: Send extra headers extra_query: Add additional query parameters to the request @@ -344,6 +388,9 @@ async def retrieve( extra_query=extra_query, extra_body=extra_body, timeout=timeout, + query=await async_maybe_transform( + {"include_disabled": include_disabled}, api_key_retrieve_params.APIKeyRetrieveParams + ), security={"admin_api_key_auth": True}, ), cast_to=ProjectAPIKey, @@ -442,6 +489,7 @@ def list( after: str | Omit = omit, limit: int | Omit = omit, owner_project_access: Literal["active", "inactive", "any"] | Omit = omit, + status: Literal["active", "inactive", "all"] | Omit = omit, # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. # The extra values given here take precedence over values defined on the client or passed to this method. extra_headers: Headers | None = None, @@ -449,8 +497,18 @@ def list( extra_body: Body | None = None, timeout: float | httpx2.Timeout | None | NotGiven = not_given, ) -> AsyncPaginator[ProjectAPIKey, AsyncConversationCursorPage[ProjectAPIKey]]: - """ - Returns a list of API keys in the project. + """Returns a list of API keys in the project. + + Use `status` to list active, + inactive, or all retained keys. Inactive keys include expired, disabled, and + revoked keys. Omitting `status` preserves the existing results and ordering, + which can include expired keys. + + Status-filtered lists may take time to reflect changes, including revocations. + Keep filters unchanged between pages; each page is a separate read. To check a + known key's current retained metadata, use the retrieve endpoint with + `include_disabled=true`. Keys whose owner or redacted value is no longer + retained are omitted. Args: after: A cursor for use in pagination. `after` is an object ID that defines your place @@ -463,9 +521,14 @@ def list( owner_project_access: Filter API keys by whether the owner currently has effective access to the project. Use `active` for owners with access, `inactive` for owners without - access, or `any` for all enabled project API keys. If omitted, the endpoint - applies its existing membership-based visibility rules, which may exclude some - enabled keys. + access, or `any` to include retained keys whose owners have lost access. This + filter is independent of the key's `status`. If omitted, the endpoint applies + its existing membership-based visibility rules. + + status: Filter by the credential's status, independently of its owner's project access. + Use `active` for enabled, unexpired keys, `inactive` for expired, disabled, or + revoked keys, or `all` for both. If omitted, existing results and ordering are + preserved, which can include expired keys. extra_headers: Send extra headers @@ -490,6 +553,7 @@ def list( "after": after, "limit": limit, "owner_project_access": owner_project_access, + "status": status, }, api_key_list_params.APIKeyListParams, ), diff --git a/src/openai/types/admin/organization/projects/__init__.py b/src/openai/types/admin/organization/projects/__init__.py index 057d928070..7cd6884aaf 100644 --- a/src/openai/types/admin/organization/projects/__init__.py +++ b/src/openai/types/admin/organization/projects/__init__.py @@ -24,6 +24,7 @@ from .group_retrieve_params import GroupRetrieveParams as GroupRetrieveParams from .project_data_retention import ProjectDataRetention as ProjectDataRetention from .api_key_delete_response import APIKeyDeleteResponse as APIKeyDeleteResponse +from .api_key_retrieve_params import APIKeyRetrieveParams as APIKeyRetrieveParams from .certificate_list_params import CertificateListParams as CertificateListParams from .project_service_account import ProjectServiceAccount as ProjectServiceAccount from .spend_alert_list_params import SpendAlertListParams as SpendAlertListParams diff --git a/src/openai/types/admin/organization/projects/api_key_list_params.py b/src/openai/types/admin/organization/projects/api_key_list_params.py index a4f49e2e95..5cd4edc746 100644 --- a/src/openai/types/admin/organization/projects/api_key_list_params.py +++ b/src/openai/types/admin/organization/projects/api_key_list_params.py @@ -27,7 +27,15 @@ class APIKeyListParams(TypedDict, total=False): """ Filter API keys by whether the owner currently has effective access to the project. Use `active` for owners with access, `inactive` for owners without - access, or `any` for all enabled project API keys. If omitted, the endpoint - applies its existing membership-based visibility rules, which may exclude some - enabled keys. + access, or `any` to include retained keys whose owners have lost access. This + filter is independent of the key's `status`. If omitted, the endpoint applies + its existing membership-based visibility rules. + """ + + status: Literal["active", "inactive", "all"] + """Filter by the credential's status, independently of its owner's project access. + + Use `active` for enabled, unexpired keys, `inactive` for expired, disabled, or + revoked keys, or `all` for both. If omitted, existing results and ordering are + preserved, which can include expired keys. """ diff --git a/src/openai/types/admin/organization/projects/api_key_retrieve_params.py b/src/openai/types/admin/organization/projects/api_key_retrieve_params.py new file mode 100644 index 0000000000..2ebc6b942e --- /dev/null +++ b/src/openai/types/admin/organization/projects/api_key_retrieve_params.py @@ -0,0 +1,19 @@ +# File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing_extensions import Required, TypedDict + +__all__ = ["APIKeyRetrieveParams"] + + +class APIKeyRetrieveParams(TypedDict, total=False): + project_id: Required[str] + + include_disabled: bool + """ + Allow retrieval of disabled or revoked keys with retained owner and redacted + metadata. Also returns active keys whose owners have lost project access; their + `owner_project_access` is `inactive`. Defaults to `false`, preserving existing + not-found responses. + """ diff --git a/src/openai/types/admin/organization/projects/project_api_key.py b/src/openai/types/admin/organization/projects/project_api_key.py index 0b3491b5e0..f8550fd774 100644 --- a/src/openai/types/admin/organization/projects/project_api_key.py +++ b/src/openai/types/admin/organization/projects/project_api_key.py @@ -81,6 +81,12 @@ class ProjectAPIKey(BaseModel): redacted_value: str """The redacted value of the API key""" + deleted_at: Optional[int] = None + """ + The Unix timestamp (in seconds) when the key was revoked, or null if + unavailable. + """ + expires_at: Optional[int] = None """ The Unix timestamp (in seconds) when the API key expires, or null if it does not @@ -89,3 +95,5 @@ class ProjectAPIKey(BaseModel): spend_limit: Optional[ProjectAPIKeySpendLimit] = None """Represents a hard spend limit configured for a project API key.""" + + status: Optional[Literal["active", "inactive"]] = None diff --git a/tests/api_resources/admin/organization/projects/test_api_keys.py b/tests/api_resources/admin/organization/projects/test_api_keys.py index d480f7176b..f93b748305 100644 --- a/tests/api_resources/admin/organization/projects/test_api_keys.py +++ b/tests/api_resources/admin/organization/projects/test_api_keys.py @@ -29,6 +29,15 @@ def test_method_retrieve(self, client: OpenAI) -> None: ) assert_matches_type(ProjectAPIKey, api_key, path=["response"]) + @parametrize + def test_method_retrieve_with_all_params(self, client: OpenAI) -> None: + api_key = client.admin.organization.projects.api_keys.retrieve( + api_key_id="api_key_id", + project_id="project_id", + include_disabled=True, + ) + assert_matches_type(ProjectAPIKey, api_key, path=["response"]) + @parametrize def test_raw_response_retrieve(self, client: OpenAI) -> None: response = client.admin.organization.projects.api_keys.with_raw_response.retrieve( @@ -151,6 +160,7 @@ def test_method_list_with_all_params(self, client: OpenAI) -> None: after="after", limit=0, owner_project_access="active", + status="active", ) assert_matches_type(SyncConversationCursorPage[ProjectAPIKey], api_key, path=["response"]) @@ -247,6 +257,15 @@ async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: ) assert_matches_type(ProjectAPIKey, api_key, path=["response"]) + @parametrize + async def test_method_retrieve_with_all_params(self, async_client: AsyncOpenAI) -> None: + api_key = await async_client.admin.organization.projects.api_keys.retrieve( + api_key_id="api_key_id", + project_id="project_id", + include_disabled=True, + ) + assert_matches_type(ProjectAPIKey, api_key, path=["response"]) + @parametrize async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: response = await async_client.admin.organization.projects.api_keys.with_raw_response.retrieve( @@ -369,6 +388,7 @@ async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> N after="after", limit=0, owner_project_access="active", + status="active", ) assert_matches_type(AsyncConversationCursorPage[ProjectAPIKey], api_key, path=["response"]) diff --git a/tests/test_project_api_key_status.py b/tests/test_project_api_key_status.py new file mode 100644 index 0000000000..1fe37d01e5 --- /dev/null +++ b/tests/test_project_api_key_status.py @@ -0,0 +1,72 @@ +import httpx2 as httpx + +from openai import OpenAI + + +def test_project_api_key_status_wire() -> None: + key = { + "object": "organization.project.api_key", + "redacted_value": "example-only", + "name": "Synthetic key", + "created_at": 1711471533, + "last_used_at": 1711471534, + "id": "key_test", + "owner_project_access": "active", + "owner": { + "type": "user", + "user": { + "id": "user_test", + "name": "Example User", + "email": "user@example.invalid", + "role": "owner", + "created_at": 1711471533, + }, + }, + } + populated = {**key, "status": "inactive", "deleted_at": 1711888017} + explicit_null = {**key, "status": "active", "deleted_at": None} + replies = iter( + [ + ("/organization/projects/proj_test/api_keys/key_test", {"include_disabled": "true"}, populated), + ("/organization/projects/proj_test/api_keys/key_test", {"include_disabled": "false"}, explicit_null), + ("/organization/projects/proj_test/api_keys/key_test", {}, key), + ( + "/organization/projects/proj_test/api_keys", + {"status": "inactive"}, + { + "object": "list", + "data": [populated], + "first_id": "key_test", + "last_id": "key_test", + "has_more": False, + }, + ), + ] + ) + + def handle(request: httpx.Request) -> httpx.Response: + path, query, payload = next(replies) + assert request.method == "GET" and request.url.path == path + assert dict(request.url.params) == query + assert request.headers["Authorization"] == "Bearer synthetic-admin-key" + return httpx.Response(200, json=payload) + + with OpenAI( + base_url="https://example.invalid", + api_key="synthetic-key", + admin_api_key="synthetic-admin-key", + http_client=httpx.Client(transport=httpx.MockTransport(handle)), + max_retries=0, + _strict_response_validation=True, + ) as client: + keys = client.admin.organization.projects.api_keys + row = keys.retrieve("key_test", project_id="proj_test", include_disabled=True) + assert row.status == "inactive" and row.deleted_at == 1711888017 + row = keys.retrieve("key_test", project_id="proj_test", include_disabled=False) + assert row.status == "active" and row.deleted_at is None and "deleted_at" in row.model_fields_set + row = keys.retrieve("key_test", project_id="proj_test") + assert row.status is None and row.deleted_at is None + assert not {"status", "deleted_at"} & row.model_fields_set + row = keys.list("proj_test", status="inactive").data[0] + assert row.status == "inactive" and row.deleted_at == 1711888017 + assert next(replies, None) is None