Skip to content

Commit f46438b

Browse files
feat: Add API key rotate endpoint
1 parent 3489c13 commit f46438b

6 files changed

Lines changed: 65 additions & 4 deletions

File tree

.stats.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 119
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel/kernel-c98841235b0ece0591f28f7dd424339b6ef2f3e8f539b95b670ae0da2ef43df4.yml
3-
openapi_spec_hash: c1e9456765f0743a333af297d135d5cf
4-
config_hash: 57567e00b41af47cef1b78e51b747aa0
1+
configured_endpoints: 120
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel/kernel-1f10598afa01d76d22b0ed63685248f482f74c9353cffe1d3e4a3d38da7716cf.yml
3+
openapi_spec_hash: 8936f458bfa681b709e459ca1cc76fb5
4+
config_hash: 03c7e57f268c750e2415831662e95969

api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ Methods:
403403
- <code title="patch /org/api_keys/{id}">client.apiKeys.<a href="./src/resources/api-keys.ts">update</a>(id, { ...params }) -> APIKey</code>
404404
- <code title="get /org/api_keys">client.apiKeys.<a href="./src/resources/api-keys.ts">list</a>({ ...params }) -> APIKeysOffsetPagination</code>
405405
- <code title="delete /org/api_keys/{id}">client.apiKeys.<a href="./src/resources/api-keys.ts">delete</a>(id) -> void</code>
406+
- <code title="post /org/api_keys/{id}/rotate">client.apiKeys.<a href="./src/resources/api-keys.ts">rotate</a>(id, { ...params }) -> CreatedAPIKey</code>
406407

407408
# CredentialProviders
408409

src/client.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
APIKeyCreateParams,
2525
APIKeyListParams,
2626
APIKeyRetrieveParams,
27+
APIKeyRotateParams,
2728
APIKeyUpdateParams,
2829
APIKeys,
2930
APIKeysOffsetPagination,
@@ -1162,6 +1163,7 @@ export declare namespace Kernel {
11621163
type APIKeyRetrieveParams as APIKeyRetrieveParams,
11631164
type APIKeyUpdateParams as APIKeyUpdateParams,
11641165
type APIKeyListParams as APIKeyListParams,
1166+
type APIKeyRotateParams as APIKeyRotateParams,
11651167
};
11661168

11671169
export {

src/resources/api-keys.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ export class APIKeys extends APIResource {
8888
headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),
8989
});
9090
}
91+
92+
/**
93+
* Rotate an API key. Issues a new key that copies the name and project of the
94+
* rotated key, and schedules the rotated key to expire after a grace period so
95+
* in-flight callers can swap over. The new plaintext key is returned once.
96+
*
97+
* @example
98+
* ```ts
99+
* const createdAPIKey = await client.apiKeys.rotate('id');
100+
* ```
101+
*/
102+
rotate(
103+
id: string,
104+
body: APIKeyRotateParams | null | undefined = {},
105+
options?: RequestOptions,
106+
): APIPromise<CreatedAPIKey> {
107+
return this._client.post(path`/org/api_keys/${id}/rotate`, { body, ...options });
108+
}
91109
}
92110

93111
export type APIKeysOffsetPagination = OffsetPagination<APIKey>;
@@ -230,6 +248,20 @@ export interface APIKeyListParams extends OffsetPaginationParams {
230248
sort_direction?: 'asc' | 'desc';
231249
}
232250

251+
export interface APIKeyRotateParams {
252+
/**
253+
* Lifetime in days for the new key, up to 3650. Omit to reuse the rotated key's
254+
* original lifetime, or never-expires if it had none.
255+
*/
256+
days_to_expire?: number | null;
257+
258+
/**
259+
* Grace period in days before the rotated key expires. Use 0 to expire it
260+
* immediately. Omit for the default grace period of 7 days.
261+
*/
262+
expire_in_days?: number | null;
263+
}
264+
233265
export declare namespace APIKeys {
234266
export {
235267
type APIKey as APIKey,
@@ -239,5 +271,6 @@ export declare namespace APIKeys {
239271
type APIKeyRetrieveParams as APIKeyRetrieveParams,
240272
type APIKeyUpdateParams as APIKeyUpdateParams,
241273
type APIKeyListParams as APIKeyListParams,
274+
type APIKeyRotateParams as APIKeyRotateParams,
242275
};
243276
}

src/resources/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export {
99
type APIKeyRetrieveParams,
1010
type APIKeyUpdateParams,
1111
type APIKeyListParams,
12+
type APIKeyRotateParams,
1213
type APIKeysOffsetPagination,
1314
} from './api-keys';
1415
export {

tests/api-resources/api-keys.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,28 @@ describe('resource apiKeys', () => {
107107
expect(dataAndResponse.data).toBe(response);
108108
expect(dataAndResponse.response).toBe(rawResponse);
109109
});
110+
111+
// Mock server tests are disabled
112+
test.skip('rotate', async () => {
113+
const responsePromise = client.apiKeys.rotate('id');
114+
const rawResponse = await responsePromise.asResponse();
115+
expect(rawResponse).toBeInstanceOf(Response);
116+
const response = await responsePromise;
117+
expect(response).not.toBeInstanceOf(Response);
118+
const dataAndResponse = await responsePromise.withResponse();
119+
expect(dataAndResponse.data).toBe(response);
120+
expect(dataAndResponse.response).toBe(rawResponse);
121+
});
122+
123+
// Mock server tests are disabled
124+
test.skip('rotate: request options and params are passed correctly', async () => {
125+
// ensure the request options are being passed correctly by passing an invalid HTTP method in order to cause an error
126+
await expect(
127+
client.apiKeys.rotate(
128+
'id',
129+
{ days_to_expire: 30, expire_in_days: 7 },
130+
{ path: '/_stainless_unknown_path' },
131+
),
132+
).rejects.toThrow(Kernel.NotFoundError);
133+
});
110134
});

0 commit comments

Comments
 (0)