Skip to content

Commit 1f05e33

Browse files
feat(api): support updating project API key expiry (#4066)
## Summary Adds support for updating an existing project API key’s expiration. ## Changes - Add client.admin.organization.projects.api_keys.update to set an expiry relative to the request time. - Require a project ID, API key ID, and expires_in_seconds value. - Return the updated project API key resource. Co-authored-by: jbeckwith-oai <[email protected]>
1 parent 0f73f1d commit 1f05e33

8 files changed

Lines changed: 373 additions & 12 deletions

File tree

‎.castiron.stats.yml‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
schema_version: 1
2-
generation_id: db47ecf5-a60b-45ff-86f3-8114509cd0d9
3-
openapi_spec_hash: c295492f5208f270ccbdc25ef7fdf5d3
4-
openapi_transformed_spec_hash: 6a1e89823137330a52308bc11b210f74
5-
config_hash: a8f4a4eaaac9054dce1407d5b3c99eaa
6-
codegen_sha: 74ba9154ddf4ef6b8fc6053a01eb404bb9cc8bcf
7-
codegen_hash: 228104162ccb4d69e01cc325a7f662095052448726ddc3f005c1fe0b7136dc2d
8-
public_codegen_sha: 3c5059f669ef2f0a45b9893228da12e9c0b9d567
2+
generation_id: 4dbefa31-3327-46e9-8f21-2ba5a07c975b
3+
openapi_spec_hash: 956d02d9e8fbe439b1409c609f26c276
4+
openapi_transformed_spec_hash: 86aa9a172bbb1e2bbc5129c37958862b
5+
config_hash: 47b922b06d16fe0e7595c5eacbbb5495
6+
codegen_sha: cb4f39401d5099f2a0d769d20f96bb76bac5a46d
7+
codegen_hash: d0f5b556f24fed3f69edaf9d4f181c306948f986b8876fde94f38a0f4511faab
8+
public_codegen_sha: a457b68efa244472e9af2d5f09494c30b184d9d1

‎api.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,7 @@ from openai.types.admin.organization.projects import ProjectAPIKey, APIKeyDelete
17311731
Methods:
17321732

17331733
- <code title="get /organization/projects/{project_id}/api_keys/{api_key_id}">client.admin.organization.projects.api_keys.<a href="./src/openai/resources/admin/organization/projects/api_keys.py">retrieve</a>(api_key_id, \*, project_id) -> <a href="./src/openai/types/admin/organization/projects/project_api_key.py">ProjectAPIKey</a></code>
1734+
- <code title="post /organization/projects/{project_id}/api_keys/{api_key_id}">client.admin.organization.projects.api_keys.<a href="./src/openai/resources/admin/organization/projects/api_keys.py">update</a>(api_key_id, \*, project_id, \*\*<a href="src/openai/types/admin/organization/projects/api_key_update_params.py">params</a>) -> <a href="./src/openai/types/admin/organization/projects/project_api_key.py">ProjectAPIKey</a></code>
17341735
- <code title="get /organization/projects/{project_id}/api_keys">client.admin.organization.projects.api_keys.<a href="./src/openai/resources/admin/organization/projects/api_keys.py">list</a>(project_id, \*\*<a href="src/openai/types/admin/organization/projects/api_key_list_params.py">params</a>) -> <a href="./src/openai/types/admin/organization/projects/project_api_key.py">SyncConversationCursorPage[ProjectAPIKey]</a></code>
17351736
- <code title="delete /organization/projects/{project_id}/api_keys/{api_key_id}">client.admin.organization.projects.api_keys.<a href="./src/openai/resources/admin/organization/projects/api_keys.py">delete</a>(api_key_id, \*, project_id) -> <a href="./src/openai/types/admin/organization/projects/api_key_delete_response.py">APIKeyDeleteResponse</a></code>
17361737

‎api_reference/openapi.transformed.yml‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12954,6 +12954,100 @@ paths:
1295412954
"id": "key_abc",
1295512955
"deleted": true
1295612956
}
12957+
post:
12958+
security:
12959+
- AdminApiKeyAuth: []
12960+
summary: Update project API key
12961+
description: |
12962+
Updates the expiration of an existing API key in the project, including a
12963+
service-account-owned key. Requires permission to write project API keys.
12964+
12965+
Provide `expires_in_seconds` to set the expiration relative to this request.
12966+
The value must satisfy the organization's and project's expiry policies.
12967+
Expired keys cannot be updated. Retrying resets the expiration relative to
12968+
the new request. The response contains only the redacted key, not its secret.
12969+
operationId: update-project-api-key
12970+
tags:
12971+
- Projects
12972+
parameters:
12973+
- name: project_id
12974+
in: path
12975+
description: The ID of the project.
12976+
required: true
12977+
schema:
12978+
type: string
12979+
- name: api_key_id
12980+
in: path
12981+
description: The ID of the API key.
12982+
required: true
12983+
schema:
12984+
type: string
12985+
requestBody:
12986+
required: true
12987+
content:
12988+
application/json:
12989+
schema:
12990+
$ref: '#/components/schemas/UpdateProjectApiKeyBody'
12991+
responses:
12992+
'200':
12993+
description: Project API key expiration updated successfully.
12994+
content:
12995+
application/json:
12996+
schema:
12997+
$ref: '#/components/schemas/ProjectApiKey'
12998+
'400':
12999+
description: The project is archived, the key is expired, or the requested expiration is invalid.
13000+
content:
13001+
application/json:
13002+
schema:
13003+
$ref: '#/components/schemas/ErrorResponse'
13004+
'403':
13005+
description: The caller does not have permission to update this API key.
13006+
content:
13007+
application/json:
13008+
schema:
13009+
$ref: '#/components/schemas/PermissionErrorResponse'
13010+
'404':
13011+
description: The project or API key was not found.
13012+
content:
13013+
application/json:
13014+
schema:
13015+
$ref: '#/components/schemas/ErrorResponse'
13016+
'429':
13017+
description: Too many management requests. Reduce the request rate and try again later.
13018+
content:
13019+
application/json:
13020+
schema:
13021+
$ref: '#/components/schemas/ErrorResponse'
13022+
x-oaiMeta:
13023+
group: administration
13024+
examples:
13025+
request:
13026+
curl: |
13027+
curl -X POST https://api.openai.com/v1/organization/projects/proj_abc/api_keys/key_abc \
13028+
-H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
13029+
-H "Content-Type: application/json" \
13030+
-d '{"expires_in_seconds": 86400}'
13031+
response: |
13032+
{
13033+
"object": "organization.project.api_key",
13034+
"redacted_value": "sk-abc...def",
13035+
"name": "My API Key",
13036+
"created_at": 1711471533,
13037+
"expires_at": 1711557933,
13038+
"last_used_at": null,
13039+
"id": "key_abc",
13040+
"owner_project_access": "active",
13041+
"owner": {
13042+
"type": "service_account",
13043+
"service_account": {
13044+
"id": "svc_acct_abc",
13045+
"name": "My Service Account",
13046+
"role": "member",
13047+
"created_at": 1711471533
13048+
}
13049+
}
13050+
}
1295713051
/organization/projects/{project_id}/archive:
1295813052
post:
1295913053
summary: Archive project
@@ -33651,6 +33745,8 @@ paths:
3365133745
application/json:
3365233746
schema:
3365333747
$ref: '#/components/schemas/UpdateVaultParams'
33748+
example:
33749+
name: Research vault
3365433750
required: true
3365533751
responses:
3365633752
'200':
@@ -78301,6 +78397,16 @@ components:
7830178397
- GB_STORAGE
7830278398
- AE_STORAGE
7830378399
- AE_STORAGE_PROCESSING
78400+
UpdateProjectApiKeyBody:
78401+
properties:
78402+
expires_in_seconds:
78403+
type: integer
78404+
maximum: 31536000
78405+
minimum: 1
78406+
description: Number of seconds from this request until the API key expires. Must comply with the organization and project expiry policies. Expired keys cannot be updated. Retrying resets the expiry relative to the new request.
78407+
type: object
78408+
required:
78409+
- expires_in_seconds
7830478410
SpecificProgrammaticToolCallingParam:
7830578411
properties:
7830678412
type:

‎src/openai/resources/admin/organization/projects/api_keys.py‎

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
from ..... import _legacy_response
1010
from ....._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
11-
from ....._utils import path_template, maybe_transform
11+
from ....._utils import path_template, maybe_transform, async_maybe_transform
1212
from ....._compat import cached_property
1313
from ....._resource import SyncAPIResource, AsyncAPIResource
1414
from ....._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
1515
from .....pagination import SyncConversationCursorPage, AsyncConversationCursorPage
1616
from ....._base_client import AsyncPaginator, make_request_options
17-
from .....types.admin.organization.projects import api_key_list_params
17+
from .....types.admin.organization.projects import api_key_list_params, api_key_update_params
1818
from .....types.admin.organization.projects.project_api_key import ProjectAPIKey
1919
from .....types.admin.organization.projects.api_key_delete_response import APIKeyDeleteResponse
2020

@@ -85,6 +85,62 @@ def retrieve(
8585
cast_to=ProjectAPIKey,
8686
)
8787

88+
def update(
89+
self,
90+
api_key_id: str,
91+
*,
92+
project_id: str,
93+
expires_in_seconds: int,
94+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
95+
# The extra values given here take precedence over values defined on the client or passed to this method.
96+
extra_headers: Headers | None = None,
97+
extra_query: Query | None = None,
98+
extra_body: Body | None = None,
99+
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
100+
) -> ProjectAPIKey:
101+
"""
102+
Updates the expiration of an existing API key in the project, including a
103+
service-account-owned key. Requires permission to write project API keys.
104+
105+
Provide `expires_in_seconds` to set the expiration relative to this request.
106+
The value must satisfy the organization's and project's expiry policies.
107+
Expired keys cannot be updated. Retrying resets the expiration relative to
108+
the new request. The response contains only the redacted key, not its secret.
109+
110+
Args:
111+
expires_in_seconds: Number of seconds from this request until the API key expires. Must comply with
112+
the organization and project expiry policies. Expired keys cannot be updated.
113+
Retrying resets the expiry relative to the new request.
114+
115+
extra_headers: Send extra headers
116+
117+
extra_query: Add additional query parameters to the request
118+
119+
extra_body: Add additional JSON properties to the request
120+
121+
timeout: Override the client-level default timeout for this request, in seconds
122+
"""
123+
if not project_id:
124+
raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}")
125+
if not api_key_id:
126+
raise ValueError(f"Expected a non-empty value for `api_key_id` but received {api_key_id!r}")
127+
return self._post(
128+
path_template(
129+
"/organization/projects/{project_id}/api_keys/{api_key_id}",
130+
project_id=project_id,
131+
api_key_id=api_key_id,
132+
),
133+
body=maybe_transform({"expires_in_seconds": expires_in_seconds}, api_key_update_params.APIKeyUpdateParams),
134+
options=make_request_options(
135+
extra_headers=extra_headers,
136+
extra_query=extra_query,
137+
extra_body=extra_body,
138+
timeout=timeout,
139+
security={"admin_api_key_auth": True},
140+
),
141+
cast_to=ProjectAPIKey,
142+
)
143+
88144
def list(
89145
self,
90146
project_id: str,
@@ -260,6 +316,64 @@ async def retrieve(
260316
cast_to=ProjectAPIKey,
261317
)
262318

319+
async def update(
320+
self,
321+
api_key_id: str,
322+
*,
323+
project_id: str,
324+
expires_in_seconds: int,
325+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
326+
# The extra values given here take precedence over values defined on the client or passed to this method.
327+
extra_headers: Headers | None = None,
328+
extra_query: Query | None = None,
329+
extra_body: Body | None = None,
330+
timeout: float | httpx2.Timeout | None | NotGiven = not_given,
331+
) -> ProjectAPIKey:
332+
"""
333+
Updates the expiration of an existing API key in the project, including a
334+
service-account-owned key. Requires permission to write project API keys.
335+
336+
Provide `expires_in_seconds` to set the expiration relative to this request.
337+
The value must satisfy the organization's and project's expiry policies.
338+
Expired keys cannot be updated. Retrying resets the expiration relative to
339+
the new request. The response contains only the redacted key, not its secret.
340+
341+
Args:
342+
expires_in_seconds: Number of seconds from this request until the API key expires. Must comply with
343+
the organization and project expiry policies. Expired keys cannot be updated.
344+
Retrying resets the expiry relative to the new request.
345+
346+
extra_headers: Send extra headers
347+
348+
extra_query: Add additional query parameters to the request
349+
350+
extra_body: Add additional JSON properties to the request
351+
352+
timeout: Override the client-level default timeout for this request, in seconds
353+
"""
354+
if not project_id:
355+
raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}")
356+
if not api_key_id:
357+
raise ValueError(f"Expected a non-empty value for `api_key_id` but received {api_key_id!r}")
358+
return await self._post(
359+
path_template(
360+
"/organization/projects/{project_id}/api_keys/{api_key_id}",
361+
project_id=project_id,
362+
api_key_id=api_key_id,
363+
),
364+
body=await async_maybe_transform(
365+
{"expires_in_seconds": expires_in_seconds}, api_key_update_params.APIKeyUpdateParams
366+
),
367+
options=make_request_options(
368+
extra_headers=extra_headers,
369+
extra_query=extra_query,
370+
extra_body=extra_body,
371+
timeout=timeout,
372+
security={"admin_api_key_auth": True},
373+
),
374+
cast_to=ProjectAPIKey,
375+
)
376+
263377
def list(
264378
self,
265379
project_id: str,
@@ -378,6 +492,9 @@ def __init__(self, api_keys: APIKeys) -> None:
378492
self.retrieve = _legacy_response.to_raw_response_wrapper(
379493
api_keys.retrieve,
380494
)
495+
self.update = _legacy_response.to_raw_response_wrapper(
496+
api_keys.update,
497+
)
381498
self.list = _legacy_response.to_raw_response_wrapper(
382499
api_keys.list,
383500
)
@@ -393,6 +510,9 @@ def __init__(self, api_keys: AsyncAPIKeys) -> None:
393510
self.retrieve = _legacy_response.async_to_raw_response_wrapper(
394511
api_keys.retrieve,
395512
)
513+
self.update = _legacy_response.async_to_raw_response_wrapper(
514+
api_keys.update,
515+
)
396516
self.list = _legacy_response.async_to_raw_response_wrapper(
397517
api_keys.list,
398518
)
@@ -408,6 +528,9 @@ def __init__(self, api_keys: APIKeys) -> None:
408528
self.retrieve = to_streamed_response_wrapper(
409529
api_keys.retrieve,
410530
)
531+
self.update = to_streamed_response_wrapper(
532+
api_keys.update,
533+
)
411534
self.list = to_streamed_response_wrapper(
412535
api_keys.list,
413536
)
@@ -423,6 +546,9 @@ def __init__(self, api_keys: AsyncAPIKeys) -> None:
423546
self.retrieve = async_to_streamed_response_wrapper(
424547
api_keys.retrieve,
425548
)
549+
self.update = async_to_streamed_response_wrapper(
550+
api_keys.update,
551+
)
426552
self.list = async_to_streamed_response_wrapper(
427553
api_keys.list,
428554
)

‎src/openai/types/admin/organization/projects/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .project_spend_limit import ProjectSpendLimit as ProjectSpendLimit
2020
from .role_delete_response import RoleDeleteResponse as RoleDeleteResponse
2121
from .user_delete_response import UserDeleteResponse as UserDeleteResponse
22+
from .api_key_update_params import APIKeyUpdateParams as APIKeyUpdateParams
2223
from .group_delete_response import GroupDeleteResponse as GroupDeleteResponse
2324
from .group_retrieve_params import GroupRetrieveParams as GroupRetrieveParams
2425
from .project_data_retention import ProjectDataRetention as ProjectDataRetention
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
from typing_extensions import Required, TypedDict
6+
7+
__all__ = ["APIKeyUpdateParams"]
8+
9+
10+
class APIKeyUpdateParams(TypedDict, total=False):
11+
project_id: Required[str]
12+
13+
expires_in_seconds: Required[int]
14+
"""Number of seconds from this request until the API key expires.
15+
16+
Must comply with the organization and project expiry policies. Expired keys
17+
cannot be updated. Retrying resets the expiry relative to the new request.
18+
"""

0 commit comments

Comments
 (0)