Files
n8n/packages/nodes-base/credentials/OpenAiApi.credentials.ts
alighasami 3d5eaf9445
Some checks failed
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
first commit
2026-03-17 16:22:57 +03:30

102 lines
2.2 KiB
TypeScript

import type {
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
export class OpenAiApi implements ICredentialType {
name = 'openAiApi';
displayName = 'OpenAi';
documentationUrl = 'openai';
properties: INodeProperties[] = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
typeOptions: { password: true },
required: true,
default: '',
},
{
displayName: 'Organization ID (optional)',
name: 'organizationId',
type: 'string',
default: '',
hint: 'Only required if you belong to multiple organisations',
description:
"For users who belong to multiple organizations, you can set which organization is used for an API request. Usage from these API requests will count against the specified organization's subscription quota.",
},
{
displayName: 'Base URL',
name: 'url',
type: 'string',
default: 'https://api.openai.com/v1',
description: 'Override the default base URL for the API',
},
{
displayName: 'Add Custom Header',
name: 'header',
type: 'boolean',
default: false,
},
{
displayName: 'Header Name',
name: 'headerName',
type: 'string',
displayOptions: {
show: {
header: [true],
},
},
default: '',
},
{
displayName: 'Header Value',
name: 'headerValue',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
header: [true],
},
},
default: '',
},
];
test: ICredentialTestRequest = {
request: {
baseURL: '={{$credentials?.url}}',
url: '/models',
},
};
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
requestOptions.headers ??= {};
requestOptions.headers['Authorization'] = `Bearer ${credentials.apiKey}`;
requestOptions.headers['OpenAI-Organization'] = credentials.organizationId;
if (
credentials.header &&
typeof credentials.headerName === 'string' &&
credentials.headerName &&
typeof credentials.headerValue === 'string'
) {
requestOptions.headers[credentials.headerName] = credentials.headerValue;
}
return requestOptions;
}
}