feat(OpenAI Node): Support custom headers for model requests (#17835)

Co-authored-by: Idir Ouhab <ouhab.idir@gmail.com>
Co-authored-by: Oleg Ivaniv <me@olegivaniv.com>
This commit is contained in:
mgosal
2025-09-12 13:14:32 +01:00
committed by GitHub
parent 1e2f4217e0
commit 0b4de85a08
5 changed files with 719 additions and 13 deletions

View File

@@ -1,7 +1,8 @@
import type {
IAuthenticateGeneric,
ICredentialDataDecryptedObject,
ICredentialTestRequest,
ICredentialType,
IHttpRequestOptions,
INodeProperties,
} from 'n8n-workflow';
@@ -37,17 +38,38 @@ export class OpenAiApi implements ICredentialType {
default: 'https://api.openai.com/v1',
description: 'Override the default base URL for the API',
},
];
authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
'OpenAI-Organization': '={{$credentials.organizationId}}',
},
{
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: {
@@ -55,4 +77,23 @@ export class OpenAiApi implements ICredentialType {
url: '/models',
},
};
async authenticate(
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
requestOptions.headers = {
Authorization: 'Bearer ' + credentials.apiKey,
'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;
}
}

View File

@@ -0,0 +1,155 @@
import type { ICredentialDataDecryptedObject, IHttpRequestOptions } from 'n8n-workflow';
import { OpenAiApi } from '../OpenAiApi.credentials';
describe('OpenAiApi Credential', () => {
const openAiApi = new OpenAiApi();
it('should have correct properties', () => {
expect(openAiApi.name).toBe('openAiApi');
expect(openAiApi.displayName).toBe('OpenAi');
expect(openAiApi.documentationUrl).toBe('openAi');
expect(openAiApi.properties).toHaveLength(6);
expect(openAiApi.test.request.baseURL).toBe('={{$credentials?.url}}');
expect(openAiApi.test.request.url).toBe('/models');
});
describe('authenticate', () => {
it('should add Authorization header with API key only', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
};
const requestOptions: IHttpRequestOptions = {
headers: {},
url: '/models',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
Authorization: 'Bearer sk-test123456789',
'OpenAI-Organization': undefined,
});
});
it('should add Authorization and Organization headers', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
organizationId: 'org-123',
};
const requestOptions: IHttpRequestOptions = {
headers: {},
url: '/models',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
Authorization: 'Bearer sk-test123456789',
'OpenAI-Organization': 'org-123',
});
});
it('should add custom header when header toggle is enabled', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
organizationId: 'org-123',
header: true,
headerName: 'X-Custom-Header',
headerValue: 'custom-value-123',
};
const requestOptions: IHttpRequestOptions = {
headers: {},
url: '/models',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
Authorization: 'Bearer sk-test123456789',
'OpenAI-Organization': 'org-123',
'X-Custom-Header': 'custom-value-123',
});
});
it('should not add custom header when header toggle is disabled', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
header: false,
headerName: 'X-Custom-Header',
headerValue: 'custom-value-123',
};
const requestOptions: IHttpRequestOptions = {
headers: {},
url: '/models',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
Authorization: 'Bearer sk-test123456789',
'OpenAI-Organization': undefined,
});
expect(result.headers?.['X-Custom-Header']).toBeUndefined();
});
it('should preserve existing headers', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
header: true,
headerName: 'X-Custom-Header',
headerValue: 'custom-value-123',
};
const requestOptions: IHttpRequestOptions = {
url: '/models',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
const raw =
typeof (result.headers as any)?.get === 'function'
? Object.fromEntries((result.headers as unknown as Headers).entries())
: (result.headers as Record<string, string | undefined>);
const headers = Object.fromEntries(Object.entries(raw).map(([k, v]) => [k.toLowerCase(), v]));
expect(headers).toEqual(
expect.objectContaining({
authorization: 'Bearer sk-test123456789',
'x-custom-header': 'custom-value-123',
'openai-organization': undefined,
}),
);
});
it('should handle empty organization ID', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
organizationId: '',
};
const requestOptions: IHttpRequestOptions = {
headers: {},
url: '/models',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
Authorization: 'Bearer sk-test123456789',
'OpenAI-Organization': '',
});
});
});
});