-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi-keys.ts
More file actions
276 lines (242 loc) · 6.74 KB
/
Copy pathapi-keys.ts
File metadata and controls
276 lines (242 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { APIResource } from '../core/resource';
import { APIPromise } from '../core/api-promise';
import { OffsetPagination, type OffsetPaginationParams, PagePromise } from '../core/pagination';
import { buildHeaders } from '../internal/headers';
import { RequestOptions } from '../internal/request-options';
import { path } from '../internal/utils/path';
/**
* Create and manage API keys for organization and project-scoped access.
*/
export class APIKeys extends APIResource {
/**
* Create a new API key within the authenticated organization.
*
* @example
* ```ts
* const createdAPIKey = await client.apiKeys.create({
* name: 'staging',
* });
* ```
*/
create(body: APIKeyCreateParams, options?: RequestOptions): APIPromise<CreatedAPIKey> {
return this._client.post('/org/api_keys', { body, ...options });
}
/**
* Retrieve an API key by ID for the authenticated organization. API keys are
* masked.
*
* @example
* ```ts
* const apiKey = await client.apiKeys.retrieve('id');
* ```
*/
retrieve(
id: string,
query: APIKeyRetrieveParams | null | undefined = {},
options?: RequestOptions,
): APIPromise<APIKey> {
return this._client.get(path`/org/api_keys/${id}`, { query, ...options });
}
/**
* Update an API key's name.
*
* @example
* ```ts
* const apiKey = await client.apiKeys.update('id', {
* name: 'new-api-name',
* });
* ```
*/
update(id: string, body: APIKeyUpdateParams, options?: RequestOptions): APIPromise<APIKey> {
return this._client.patch(path`/org/api_keys/${id}`, { body, ...options });
}
/**
* List API keys for the authenticated organization. API keys are masked.
*
* @example
* ```ts
* // Automatically fetches more pages as needed.
* for await (const apiKey of client.apiKeys.list()) {
* // ...
* }
* ```
*/
list(
query: APIKeyListParams | null | undefined = {},
options?: RequestOptions,
): PagePromise<APIKeysOffsetPagination, APIKey> {
return this._client.getAPIList('/org/api_keys', OffsetPagination<APIKey>, { query, ...options });
}
/**
* Delete an API key.
*
* @example
* ```ts
* await client.apiKeys.delete('id');
* ```
*/
delete(id: string, options?: RequestOptions): APIPromise<void> {
return this._client.delete(path`/org/api_keys/${id}`, {
...options,
headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),
});
}
/**
* Rotate an API key. Issues a new key that copies the name and project of the
* rotated key, and schedules the rotated key to expire after a grace period so
* in-flight callers can swap over. The new plaintext key is returned once.
*
* @example
* ```ts
* const createdAPIKey = await client.apiKeys.rotate('id');
* ```
*/
rotate(
id: string,
body: APIKeyRotateParams | null | undefined = {},
options?: RequestOptions,
): APIPromise<CreatedAPIKey> {
return this._client.post(path`/org/api_keys/${id}/rotate`, { body, ...options });
}
}
export type APIKeysOffsetPagination = OffsetPagination<APIKey>;
export interface APIKey {
/**
* Unique API key identifier
*/
id: string;
/**
* When the API key was created
*/
created_at: string;
created_by: APIKey.CreatedBy;
/**
* When the API key was deleted (soft-deleted). Null for keys that have not been
* deleted.
*/
deleted_at: string | null;
/**
* When the API key expires
*/
expires_at: string | null;
/**
* Masked version of the API key
*/
masked_key: string;
/**
* API key name
*/
name: string;
/**
* Project identifier for project-scoped API keys. Null means org-wide.
*/
project_id: string | null;
/**
* Project name for project-scoped API keys. Null means the key is org-wide or the
* project name is unavailable.
*/
project_name: string | null;
}
export namespace APIKey {
export interface CreatedBy {
/**
* Kernel user ID of the creator.
*/
id: string;
/**
* Email address of the creator.
*/
email: string;
/**
* Display name of the creator, if available.
*/
name: string | null;
}
}
/**
* API key returned immediately after creation. Includes the plaintext key once.
*/
export interface CreatedAPIKey extends APIKey {
/**
* Plaintext API key. Only returned once when the key is created.
*/
key: string;
}
export interface APIKeyCreateParams {
/**
* API key name (1-255 characters)
*/
name: string;
/**
* Number of days until expiry, up to 3650. Use null for never.
*/
days_to_expire?: number | null;
/**
* Unique project identifier
*/
project_id?: string | null;
}
export interface APIKeyRetrieveParams {
/**
* When true, return the API key even if it has been deleted (soft-deleted), for
* audit purposes. Defaults to false, which returns 404 for a deleted key.
*/
include_deleted?: boolean;
}
export interface APIKeyUpdateParams {
/**
* New API key name
*/
name: string;
}
export interface APIKeyListParams extends OffsetPaginationParams {
/**
* Deprecated: use status=all instead. When true, include deleted (soft-deleted)
* API keys in the results for audit purposes.
*/
include_deleted?: boolean;
/**
* Case-insensitive substring match against API key name, creator, and project. API
* key identifiers and masked keys match by exact value or prefix.
*/
query?: string;
/**
* Field to sort API keys by.
*/
sort_by?: 'created_at' | 'name' | 'expires_at';
/**
* Sort direction for API keys.
*/
sort_direction?: 'asc' | 'desc';
/**
* Filter API keys by status. "active" returns keys that are not deleted (default;
* expired-but-not-deleted keys are still included), "deleted" returns only
* soft-deleted keys, "all" returns both.
*/
status?: 'active' | 'deleted' | 'all';
}
export interface APIKeyRotateParams {
/**
* Lifetime in days for the new key, up to 3650. Omit to reuse the rotated key's
* original lifetime, or never-expires if it had none.
*/
days_to_expire?: number | null;
/**
* Grace period in days before the rotated key expires. Use 0 to expire it
* immediately. Omit for the default grace period of 7 days.
*/
expire_in_days?: number | null;
}
export declare namespace APIKeys {
export {
type APIKey as APIKey,
type CreatedAPIKey as CreatedAPIKey,
type APIKeysOffsetPagination as APIKeysOffsetPagination,
type APIKeyCreateParams as APIKeyCreateParams,
type APIKeyRetrieveParams as APIKeyRetrieveParams,
type APIKeyUpdateParams as APIKeyUpdateParams,
type APIKeyListParams as APIKeyListParams,
type APIKeyRotateParams as APIKeyRotateParams,
};
}