Files
Abimael Martell 8845a2fc08 Add support for local/custom API URLs (#20)
* Add support for local/custom API URLs

For custom API URLs (e.g., local development with Docker), the CLI now:
- Skips API key requirement when using non-cloud URLs
- Allows optional API key input during login
- Skips browser auth flow for custom URLs

This enables use with local Firecrawl instances that have
USE_DB_AUTHENTICATION=false.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Add --api-url option to commands

Add --api-url flag at both global and command levels (scrape, crawl,
map, search, credit-usage) similar to --api-key. When a custom API URL
is provided, authentication is skipped allowing requests to local or
self-hosted Firecrawl instances without an API key.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: Add --api-url option documentation

Document the --api-url option for self-hosted and local development
use cases in the README, including examples for environment variables
and CI/CD usage.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: Add --api-url to login and config examples

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 16:48:27 -08:00

109 lines
2.7 KiB
TypeScript

/**
* Global configuration system
*/
import { loadCredentials } from './credentials';
export interface GlobalConfig {
apiKey?: string;
apiUrl?: string;
timeoutMs?: number;
maxRetries?: number;
backoffFactor?: number;
}
/**
* Global configuration instance
*/
let globalConfig: GlobalConfig = {};
/**
* Initialize global configuration
* Loads from: provided config > environment variables > OS credential storage
* @param config Configuration options
*/
export function initializeConfig(config: Partial<GlobalConfig> = {}): void {
// Priority: provided config > env vars > stored credentials
const storedCredentials = loadCredentials();
globalConfig = {
apiKey:
config.apiKey ||
process.env.FIRECRAWL_API_KEY ||
storedCredentials?.apiKey,
apiUrl:
config.apiUrl ||
process.env.FIRECRAWL_API_URL ||
storedCredentials?.apiUrl,
timeoutMs: config.timeoutMs,
maxRetries: config.maxRetries,
backoffFactor: config.backoffFactor,
};
}
/**
* Get the current global configuration
*/
export function getConfig(): GlobalConfig {
return { ...globalConfig };
}
/**
* Update global configuration (merges with existing)
*/
export function updateConfig(config: Partial<GlobalConfig>): void {
globalConfig = {
...globalConfig,
...config,
};
}
/**
* Get API key from global config or provided value
* Priority: provided key > global config > env var > stored credentials
*/
export function getApiKey(providedKey?: string): string | undefined {
if (providedKey) return providedKey;
if (globalConfig.apiKey) return globalConfig.apiKey;
if (process.env.FIRECRAWL_API_KEY) return process.env.FIRECRAWL_API_KEY;
// Fallback to stored credentials if not already loaded
const storedCredentials = loadCredentials();
return storedCredentials?.apiKey;
}
const DEFAULT_API_URL = 'https://api.firecrawl.dev';
/**
* Check if using a custom (non-cloud) API URL
*/
export function isCustomApiUrl(apiUrl?: string): boolean {
const url = apiUrl || globalConfig.apiUrl;
return !!url && url !== DEFAULT_API_URL;
}
/**
* Validate that required configuration is present
* API key is only required for the cloud API, not for local/custom APIs
*/
export function validateConfig(apiKey?: string): void {
// Skip API key validation for custom API URLs (e.g., local development)
if (isCustomApiUrl()) {
return;
}
const key = getApiKey(apiKey);
if (!key) {
throw new Error(
'API key is required. Set FIRECRAWL_API_KEY environment variable, use --api-key flag, or run "firecrawl config" to set the API key.'
);
}
}
/**
* Reset global configuration (useful for testing)
*/
export function resetConfig(): void {
globalConfig = {};
}