{"record":{"id":"c7f62a3a69caf9c9","repo":"mastra-ai/mastra","slug":"tavily-api-key-is-required-pass-apikey-or-set","errorCode":null,"errorMessage":"Tavily API key is required. Pass { apiKey } or set TAVILY_API_KEY env var.","messagePattern":"Tavily API key is required\\. Pass (.+?) or set TAVILY_API_KEY env var\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"integrations/tavily/src/client.ts","lineNumber":11,"sourceCode":"import { tavily } from '@tavily/core';\nimport type { TavilyClientOptions } from '@tavily/core';\n\nexport type { TavilyClientOptions };\n\nexport type TavilyClient = ReturnType<typeof tavily>;\n\nexport function getTavilyClient(config?: TavilyClientOptions): TavilyClient {\n  const apiKey = config?.apiKey ?? process.env.TAVILY_API_KEY;\n  if (!apiKey) {\n    throw new Error('Tavily API key is required. Pass { apiKey } or set TAVILY_API_KEY env var.');\n  }\n  // defaulting `clientName` to `mastra` if not provided\n  return tavily({ ...config, apiKey, clientName: config?.clientName ?? 'mastra' });\n}\n","sourceCodeStart":1,"sourceCodeEnd":16,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/integrations/tavily/src/client.ts#L1-L16","documentation":"getTavilyClient resolves the API key from config or the TAVILY_API_KEY env var and throws if neither is set, because all Tavily API calls require authentication.","triggerScenarios":"Calling getTavilyClient() or any tool/factory that calls it (client, getClient) with no config.apiKey while process.env.TAVILY_API_KEY is undefined.","commonSituations":"Missing .env or dotenv not invoked before client creation; key configured under a different var name; serverless deploy where env vars weren't attached to the function.","solutions":["Set TAVILY_API_KEY in the environment.","Pass { apiKey } explicitly to getTavilyClient (e.g. from a secrets manager).","Ensure .env loading happens before the first getTavilyClient call."],"exampleFix":"// before\nconst tavily = getTavilyClient();\n// after\nconst tavily = getTavilyClient({ apiKey: process.env.TAVILY_API_KEY! });","handlingStrategy":"validation","validationCode":"const apiKey = config?.apiKey ?? process.env.TAVILY_API_KEY;\nif (!apiKey) throw new Error('Set TAVILY_API_KEY before creating the Tavily client');","typeGuard":"function hasTavilyApiKey(config?: { apiKey?: string }): boolean {\n  return typeof config?.apiKey === 'string' && config.apiKey.length > 0 || !!process.env.TAVILY_API_KEY;\n}","tryCatchPattern":"try {\n  const tavily = getTavilyClient();\n} catch (e) {\n  if ((e as Error).message.includes('Tavily API key is required')) {\n    throw new Error('TAVILY_API_KEY not configured in this environment');\n  }\n  throw e;\n}","preventionTips":["Load .env before client creation and assert TAVILY_API_KEY at boot","Add TAVILY_API_KEY to serverless/CI environment configuration","Prefer explicit { apiKey } injection from a secrets manager in production"],"tags":["api-key","configuration","environment","tavily"],"backgroundTag":"missing-api-key","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}