Skip to content

Commit 2ad9913

Browse files
feat: make api key optional in sdk
1 parent 6f9e60b commit 2ad9913

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 115
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/letta-ai%2Fletta-sdk-4839625a63601d0e909ceeac53283a9b56d81464a10a95cd6c0cf5959ef3408b.yml
33
openapi_spec_hash: 51fbc7ba45b9ff929e7d68b9dea32dba
4-
config_hash: d290a6d98369e3135a785ad352fb2996
4+
config_hash: 9add368b1f37ff4e27f59d9d065e3a36

src/client.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export interface ClientOptions {
199199
/**
200200
* Defaults to process.env['LETTA_API_KEY'].
201201
*/
202-
apiKey?: string | undefined;
202+
apiKey?: string | null | undefined;
203203

204204
projectID?: string | null | undefined;
205205

@@ -290,7 +290,7 @@ export interface ClientOptions {
290290
* API Client for interfacing with the Letta API.
291291
*/
292292
export class Letta {
293-
apiKey: string;
293+
apiKey: string | null;
294294
projectID: string | null;
295295
project: string | null;
296296

@@ -309,7 +309,7 @@ export class Letta {
309309
/**
310310
* API Client for interfacing with the Letta API.
311311
*
312-
* @param {string | undefined} [opts.apiKey=process.env['LETTA_API_KEY'] ?? undefined]
312+
* @param {string | null | undefined} [opts.apiKey=process.env['LETTA_API_KEY'] ?? null]
313313
* @param {string | null | undefined} [opts.projectID]
314314
* @param {string | null | undefined} [opts.project]
315315
* @param {Environment} [opts.environment=cloud] - Specifies the environment URL to use for the API.
@@ -323,17 +323,11 @@ export class Letta {
323323
*/
324324
constructor({
325325
baseURL = readEnv('LETTA_BASE_URL'),
326-
apiKey = readEnv('LETTA_API_KEY'),
326+
apiKey = readEnv('LETTA_API_KEY') ?? null,
327327
projectID = null,
328328
project = null,
329329
...opts
330330
}: ClientOptions = {}) {
331-
if (apiKey === undefined) {
332-
throw new Errors.LettaError(
333-
"The LETTA_API_KEY environment variable is missing or empty; either provide it, or instantiate the Letta client with an apiKey option, like new Letta({ apiKey: 'My API Key' }).",
334-
);
335-
}
336-
337331
const options: ClientOptions = {
338332
apiKey,
339333
projectID,
@@ -412,10 +406,22 @@ export class Letta {
412406
}
413407

414408
protected validateHeaders({ values, nulls }: NullableHeaders) {
415-
return;
409+
if (this.apiKey && values.get('authorization')) {
410+
return;
411+
}
412+
if (nulls.has('authorization')) {
413+
return;
414+
}
415+
416+
throw new Error(
417+
'Could not resolve authentication method. Expected the apiKey to be set. Or for the "Authorization" headers to be explicitly omitted',
418+
);
416419
}
417420

418421
protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
422+
if (this.apiKey == null) {
423+
return undefined;
424+
}
419425
return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
420426
}
421427

0 commit comments

Comments
 (0)