n8n-io/n8n · error · Error

API key must be a string

Error message

API key must be a string

What it means

The Ollama transport validates the type of the apiKey credential before using it to build the Authorization header. If apiKey is defined but is not a string (e.g. a number or object was stored in the credential), the transport throws a raw Error. This is a pre-flight type guard, not an API response error — it fires before any HTTP request is made.

Source

Thrown at packages/@n8n/nodes-langchain/nodes/vendors/Ollama/transport/index.ts:29

	qs?: IDataObject;
	option?: IDataObject;
};

export async function apiRequest(
	this: IExecuteFunctions | ILoadOptionsFunctions,
	method: IHttpRequestMethods,
	endpoint: string,
	parameters?: RequestParameters,
) {
	const { body, qs, option } = parameters ?? {};

	const credentials = await this.getCredentials<{
		apiKey?: string;
		baseUrl: string;
	}>('ollamaApi');
	const apiKey = credentials.apiKey;
	if (apiKey !== undefined && typeof apiKey !== 'string') {
		throw new Error('API key must be a string');
	}

	const url = `${credentials.baseUrl.replace(/\/$/, '')}${endpoint}`;
	const headers = parameters?.headers ?? {};
	if (apiKey) {
		headers.Authorization = `Bearer ${apiKey}`;
	}

	const options = {
		headers: {
			'Content-Type': 'application/json',
			...headers,
		},
		method,
		body,
		qs,
		url,
		json: true,

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. Delete and re-create the Ollama credential in the n8n credential editor, typing the API key as a plain string
  2. If Ollama runs without authentication (local instance), clear the apiKey field entirely so it is undefined
  3. Verify the credential JSON in the database stores apiKey as a string, not a number or object

Example fix

// before — credential has apiKey: 123456789 (number)
// after  — credential has apiKey: "123456789" (string)
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate credential apiKey type before calling apiRequest
const creds = await this.getCredentials<{ apiKey?: string; baseUrl: string }>('ollamaApi');
if (creds.apiKey !== undefined && typeof creds.apiKey !== 'string') {
  throw new UserError('Ollama API key must be a string. Please re-create the credential.');
}

Type guard

function isValidOllamaApiKey(value: unknown): value is string | undefined {
  return value === undefined || typeof value === 'string';
}

Prevention

When it happens

Trigger: The 'ollamaApi' credential's apiKey field is populated with a non-string value. This happens when credentials are set programmatically or via a corrupted credential store where apiKey is stored as a number, boolean, or object instead of a string.

Common situations: Credential data was imported or injected via API/script that serialized the key as a non-string; the credential type definition was modified to allow non-string values; a migration bug stored apiKey incorrectly.

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/7564cda8a2e05c7d. Report an issue: GitHub.