{"record":{"id":"7564cda8a2e05c7d","repo":"n8n-io/n8n","slug":"api-key-must-be-a-string","errorCode":null,"errorMessage":"API key must be a string","messagePattern":"API key must be a string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@n8n/nodes-langchain/nodes/vendors/Ollama/transport/index.ts","lineNumber":29,"sourceCode":"\tqs?: IDataObject;\n\toption?: IDataObject;\n};\n\nexport async function apiRequest(\n\tthis: IExecuteFunctions | ILoadOptionsFunctions,\n\tmethod: IHttpRequestMethods,\n\tendpoint: string,\n\tparameters?: RequestParameters,\n) {\n\tconst { body, qs, option } = parameters ?? {};\n\n\tconst credentials = await this.getCredentials<{\n\t\tapiKey?: string;\n\t\tbaseUrl: string;\n\t}>('ollamaApi');\n\tconst apiKey = credentials.apiKey;\n\tif (apiKey !== undefined && typeof apiKey !== 'string') {\n\t\tthrow new Error('API key must be a string');\n\t}\n\n\tconst url = `${credentials.baseUrl.replace(/\\/$/, '')}${endpoint}`;\n\tconst headers = parameters?.headers ?? {};\n\tif (apiKey) {\n\t\theaders.Authorization = `Bearer ${apiKey}`;\n\t}\n\n\tconst options = {\n\t\theaders: {\n\t\t\t'Content-Type': 'application/json',\n\t\t\t...headers,\n\t\t},\n\t\tmethod,\n\t\tbody,\n\t\tqs,\n\t\turl,\n\t\tjson: true,","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/nodes-langchain/nodes/vendors/Ollama/transport/index.ts#L11-L47","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Delete and re-create the Ollama credential in the n8n credential editor, typing the API key as a plain string","If Ollama runs without authentication (local instance), clear the apiKey field entirely so it is undefined","Verify the credential JSON in the database stores apiKey as a string, not a number or object"],"exampleFix":"// before — credential has apiKey: 123456789 (number)\n// after  — credential has apiKey: \"123456789\" (string)","handlingStrategy":"type-guard","validationCode":"// Validate credential apiKey type before calling apiRequest\nconst creds = await this.getCredentials<{ apiKey?: string; baseUrl: string }>('ollamaApi');\nif (creds.apiKey !== undefined && typeof creds.apiKey !== 'string') {\n  throw new UserError('Ollama API key must be a string. Please re-create the credential.');\n}","typeGuard":"function isValidOllamaApiKey(value: unknown): value is string | undefined {\n  return value === undefined || typeof value === 'string';\n}","tryCatchPattern":null,"preventionTips":["Always create Ollama credentials through the n8n credential UI to ensure type safety","If programmatically creating credentials, validate apiKey is string|undefined before persisting","Use the n8n REST API with correct types when injecting credentials"],"tags":["ollama","credentials","type-validation","pre-flight"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}