Skip to content

Commit 56c6d8b

Browse files
authored
feat (providers/fal): fall back to FAL_KEY for api key environment variable name (#5453)
1 parent c7b3f0b commit 56c6d8b

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

.changeset/brave-toes-sit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/fal': patch
3+
---
4+
5+
feat (providers/fal): fall back to FAL_KEY for api key environment variable name

content/providers/01-ai-sdk-providers/10-fal.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ If you need a customized setup, you can import `createFal` and create a provider
3737
import { createFal } from '@ai-sdk/fal';
3838

3939
const fal = createFal({
40-
apiKey: 'your-api-key', // optional, defaults to FAL_API_KEY environment variable
40+
apiKey: 'your-api-key', // optional, defaults to FAL_API_KEY environment variable, falling back to FAL_KEY
4141
baseURL: 'custom-url', // optional
4242
headers: {
4343
/* custom headers */
@@ -55,7 +55,7 @@ You can use the following optional settings to customize the Fal provider instan
5555
- **apiKey** _string_
5656

5757
API key that is being sent using the `Authorization` header.
58-
It defaults to the `FAL_API_KEY` environment variable.
58+
It defaults to the `FAL_API_KEY` environment variable, falling back to `FAL_KEY`.
5959

6060
- **headers** _Record<string,string>_
6161

packages/fal/src/fal-provider.ts

+44-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { ImageModelV1, NoSuchModelError, ProviderV1 } from '@ai-sdk/provider';
22
import type { FetchFunction } from '@ai-sdk/provider-utils';
3-
import { loadApiKey, withoutTrailingSlash } from '@ai-sdk/provider-utils';
3+
import { withoutTrailingSlash } from '@ai-sdk/provider-utils';
44
import { FalImageModel } from './fal-image-model';
55
import { FalImageModelId, FalImageSettings } from './fal-image-settings';
66

77
export interface FalProviderSettings {
88
/**
99
fal.ai API key. Default value is taken from the `FAL_API_KEY` environment
10-
variable.
10+
variable, falling back to `FAL_KEY`.
1111
*/
1212
apiKey?: string;
1313

@@ -46,16 +46,55 @@ Creates a model for image generation.
4646

4747
const defaultBaseURL = 'https://fal.run';
4848

49+
function loadFalApiKey({
50+
apiKey,
51+
description = 'fal.ai',
52+
}: {
53+
apiKey: string | undefined;
54+
description?: string;
55+
}): string {
56+
if (typeof apiKey === 'string') {
57+
return apiKey;
58+
}
59+
60+
if (apiKey != null) {
61+
throw new Error(`${description} API key must be a string.`);
62+
}
63+
64+
if (typeof process === 'undefined') {
65+
throw new Error(
66+
`${description} API key is missing. Pass it using the 'apiKey' parameter. Environment variables are not supported in this environment.`,
67+
);
68+
}
69+
70+
let envApiKey = process.env.FAL_API_KEY;
71+
if (envApiKey == null) {
72+
envApiKey = process.env.FAL_KEY;
73+
}
74+
75+
if (envApiKey == null) {
76+
throw new Error(
77+
`${description} API key is missing. Pass it using the 'apiKey' parameter or set either the FAL_API_KEY or FAL_KEY environment variable.`,
78+
);
79+
}
80+
81+
if (typeof envApiKey !== 'string') {
82+
throw new Error(
83+
`${description} API key must be a string. The value of the environment variable is not a string.`,
84+
);
85+
}
86+
87+
return envApiKey;
88+
}
89+
4990
/**
5091
Create a fal.ai provider instance.
5192
*/
5293
export function createFal(options: FalProviderSettings = {}): FalProvider {
5394
const baseURL = withoutTrailingSlash(options.baseURL ?? defaultBaseURL);
5495
const getHeaders = () => ({
55-
Authorization: `Key ${loadApiKey({
96+
Authorization: `Key ${loadFalApiKey({
5697
apiKey: options.apiKey,
57-
environmentVariableName: 'FAL_API_KEY',
58-
description: 'fal.ai',
5998
})}`,
6099
...options.headers,
61100
});

turbo.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"COHERE_API_KEY",
1616
"DEEPINFRA_API_KEY",
1717
"DEEPSEEK_API_KEY",
18+
"FAL_KEY",
1819
"FAL_API_KEY",
1920
"FIREWORKS_API_KEY",
2021
"GOOGLE_API_KEY",

0 commit comments

Comments
 (0)