|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +import { APIResource } from '../core/resource'; |
| 4 | +import { APIPromise } from '../core/api-promise'; |
| 5 | +import { OffsetPagination, type OffsetPaginationParams, PagePromise } from '../core/pagination'; |
| 6 | +import { buildHeaders } from '../internal/headers'; |
| 7 | +import { RequestOptions } from '../internal/request-options'; |
| 8 | +import { path } from '../internal/utils/path'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Create and manage API keys for organization and project-scoped access. |
| 12 | + */ |
| 13 | +export class APIKeys extends APIResource { |
| 14 | + /** |
| 15 | + * Create a new API key within the authenticated organization. |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```ts |
| 19 | + * const createdAPIKey = await client.apiKeys.create({ |
| 20 | + * name: 'staging', |
| 21 | + * }); |
| 22 | + * ``` |
| 23 | + */ |
| 24 | + create(body: APIKeyCreateParams, options?: RequestOptions): APIPromise<CreatedAPIKey> { |
| 25 | + return this._client.post('/org/api_keys', { body, ...options }); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Retrieve an API key by ID for the authenticated organization. API keys are |
| 30 | + * masked. |
| 31 | + * |
| 32 | + * @example |
| 33 | + * ```ts |
| 34 | + * const apiKey = await client.apiKeys.retrieve('id'); |
| 35 | + * ``` |
| 36 | + */ |
| 37 | + retrieve(id: string, options?: RequestOptions): APIPromise<APIKey> { |
| 38 | + return this._client.get(path`/org/api_keys/${id}`, options); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Update an API key's name. |
| 43 | + * |
| 44 | + * @example |
| 45 | + * ```ts |
| 46 | + * const apiKey = await client.apiKeys.update('id', { |
| 47 | + * name: 'new-api-name', |
| 48 | + * }); |
| 49 | + * ``` |
| 50 | + */ |
| 51 | + update(id: string, body: APIKeyUpdateParams, options?: RequestOptions): APIPromise<APIKey> { |
| 52 | + return this._client.patch(path`/org/api_keys/${id}`, { body, ...options }); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * List API keys for the authenticated organization. API keys are masked. |
| 57 | + * |
| 58 | + * @example |
| 59 | + * ```ts |
| 60 | + * // Automatically fetches more pages as needed. |
| 61 | + * for await (const apiKey of client.apiKeys.list()) { |
| 62 | + * // ... |
| 63 | + * } |
| 64 | + * ``` |
| 65 | + */ |
| 66 | + list( |
| 67 | + query: APIKeyListParams | null | undefined = {}, |
| 68 | + options?: RequestOptions, |
| 69 | + ): PagePromise<APIKeysOffsetPagination, APIKey> { |
| 70 | + return this._client.getAPIList('/org/api_keys', OffsetPagination<APIKey>, { query, ...options }); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Delete an API key. |
| 75 | + * |
| 76 | + * @example |
| 77 | + * ```ts |
| 78 | + * await client.apiKeys.delete('id'); |
| 79 | + * ``` |
| 80 | + */ |
| 81 | + delete(id: string, options?: RequestOptions): APIPromise<void> { |
| 82 | + return this._client.delete(path`/org/api_keys/${id}`, { |
| 83 | + ...options, |
| 84 | + headers: buildHeaders([{ Accept: '*/*' }, options?.headers]), |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export type APIKeysOffsetPagination = OffsetPagination<APIKey>; |
| 90 | + |
| 91 | +export interface APIKey { |
| 92 | + /** |
| 93 | + * Unique API key identifier |
| 94 | + */ |
| 95 | + id: string; |
| 96 | + |
| 97 | + /** |
| 98 | + * When the API key was created |
| 99 | + */ |
| 100 | + created_at: string; |
| 101 | + |
| 102 | + created_by: APIKey.CreatedBy; |
| 103 | + |
| 104 | + /** |
| 105 | + * When the API key expires |
| 106 | + */ |
| 107 | + expires_at: string | null; |
| 108 | + |
| 109 | + /** |
| 110 | + * Masked version of the API key |
| 111 | + */ |
| 112 | + masked_key: string; |
| 113 | + |
| 114 | + /** |
| 115 | + * API key name |
| 116 | + */ |
| 117 | + name: string; |
| 118 | + |
| 119 | + /** |
| 120 | + * Project identifier for project-scoped API keys. Null means org-wide. |
| 121 | + */ |
| 122 | + project_id: string | null; |
| 123 | + |
| 124 | + /** |
| 125 | + * Project name for project-scoped API keys. Null means the key is org-wide or the |
| 126 | + * project name is unavailable. |
| 127 | + */ |
| 128 | + project_name: string | null; |
| 129 | +} |
| 130 | + |
| 131 | +export namespace APIKey { |
| 132 | + export interface CreatedBy { |
| 133 | + /** |
| 134 | + * Kernel user ID of the creator. |
| 135 | + */ |
| 136 | + id: string; |
| 137 | + |
| 138 | + /** |
| 139 | + * Email address of the creator. |
| 140 | + */ |
| 141 | + email: string; |
| 142 | + |
| 143 | + /** |
| 144 | + * Display name of the creator, if available. |
| 145 | + */ |
| 146 | + name: string | null; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +export interface CreateAPIKeyRequest { |
| 151 | + /** |
| 152 | + * API key name (1-255 characters) |
| 153 | + */ |
| 154 | + name: string; |
| 155 | + |
| 156 | + /** |
| 157 | + * Number of days until expiry, up to 3650. Use null for never. |
| 158 | + */ |
| 159 | + days_to_expire?: number | null; |
| 160 | + |
| 161 | + /** |
| 162 | + * Unique project identifier |
| 163 | + */ |
| 164 | + project_id?: string | null; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * API key returned immediately after creation. Includes the plaintext key once. |
| 169 | + */ |
| 170 | +export interface CreatedAPIKey extends APIKey { |
| 171 | + /** |
| 172 | + * Plaintext API key. Only returned once when the key is created. |
| 173 | + */ |
| 174 | + key: string; |
| 175 | +} |
| 176 | + |
| 177 | +export interface UpdateAPIKeyRequest { |
| 178 | + /** |
| 179 | + * New API key name |
| 180 | + */ |
| 181 | + name: string; |
| 182 | +} |
| 183 | + |
| 184 | +export interface APIKeyCreateParams { |
| 185 | + /** |
| 186 | + * API key name (1-255 characters) |
| 187 | + */ |
| 188 | + name: string; |
| 189 | + |
| 190 | + /** |
| 191 | + * Number of days until expiry, up to 3650. Use null for never. |
| 192 | + */ |
| 193 | + days_to_expire?: number | null; |
| 194 | + |
| 195 | + /** |
| 196 | + * Unique project identifier |
| 197 | + */ |
| 198 | + project_id?: string | null; |
| 199 | +} |
| 200 | + |
| 201 | +export interface APIKeyUpdateParams { |
| 202 | + /** |
| 203 | + * New API key name |
| 204 | + */ |
| 205 | + name: string; |
| 206 | +} |
| 207 | + |
| 208 | +export interface APIKeyListParams extends OffsetPaginationParams {} |
| 209 | + |
| 210 | +export declare namespace APIKeys { |
| 211 | + export { |
| 212 | + type APIKey as APIKey, |
| 213 | + type CreateAPIKeyRequest as CreateAPIKeyRequest, |
| 214 | + type CreatedAPIKey as CreatedAPIKey, |
| 215 | + type UpdateAPIKeyRequest as UpdateAPIKeyRequest, |
| 216 | + type APIKeysOffsetPagination as APIKeysOffsetPagination, |
| 217 | + type APIKeyCreateParams as APIKeyCreateParams, |
| 218 | + type APIKeyUpdateParams as APIKeyUpdateParams, |
| 219 | + type APIKeyListParams as APIKeyListParams, |
| 220 | + }; |
| 221 | +} |
0 commit comments