You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用Klaviyo第三方API遇403错误:需指定API密钥

Fixing that Klaviyo 403 "You must specify an API key to make request" Error

Ah, I’ve run into this exact issue with Klaviyo’s v1 API before! The problem is that your code is sending the API key in a custom api_key header, but Klaviyo’s v1 endpoint doesn’t recognize that format. Let’s get this sorted out with one of two valid approaches:


Option 1: Pass the API key as a query parameter

Klaviyo’s v1 API explicitly supports appending the API key directly to the URL as a query parameter. This is the most straightforward fix:

const { id } = req.body;
request.get({ 
  url: `https://a.klaviyo.com/api/v1/person/${id}?api_key=${process.env.KLAVIYO_API_KEY}`, 
}, (error, response, body) => { 
  // First handle any network errors
  if (error) {
    return res.json({ error: error.message, status: 500 });
  }
  
  try {
    const profile = JSON.parse(body); 
    console.log(profile); 
    
    if (response.statusCode === 200) { 
      res.json({ profile, status: 201 }); 
    } else { 
      res.json({ 
        error: 'Did not get customer data', 
        status: 500, 
        responseStatus: response.statusCode,
        responseBody: body
      }); 
    }
  } catch (parseError) {
    // Catch JSON parsing failures
    res.json({ 
      error: 'Failed to parse Klaviyo response', 
      status: 500, 
      parseError: parseError.message 
    });
  }
})

Option 2: Use the correct Authorization header format

If you prefer to keep credentials out of the URL, Klaviyo accepts the API key via an Authorization header formatted as Klaviyo-API-Key YOUR_KEY:

const { id } = req.body;
request.get({ 
  url: `https://a.klaviyo.com/api/v1/person/${id}`, 
  headers: { 
    'Authorization': `Klaviyo-API-Key ${process.env.KLAVIYO_API_KEY}` 
  } 
}, (error, response, body) => { 
  // Reuse the same error handling logic from Option 1
  if (error) {
    return res.json({ error: error.message, status: 500 });
  }
  
  try {
    const profile = JSON.parse(body); 
    console.log(profile); 
    
    if (response.statusCode === 200) { 
      res.json({ profile, status: 201 }); 
    } else { 
      res.json({ 
        error: 'Did not get customer data', 
        status: 500, 
        responseStatus: response.statusCode,
        responseBody: body
      }); 
    }
  } catch (parseError) {
    res.json({ 
      error: 'Failed to parse Klaviyo response', 
      status: 500, 
      parseError: parseError.message 
    });
  }
})

Quick Extra Tips

  • Double-check your env var: Make sure process.env.KLAVIYO_API_KEY is actually loading correctly. Toss a console.log(process.env.KLAVIYO_API_KEY) at the top of your file to verify it’s not undefined.
  • Upgrade if you can: Klaviyo’s v1 API is legacy—their v2 API uses a more standard Bearer token auth (Authorization: Bearer YOUR_KEY) and has better documentation. Worth considering if you’re able to refactor!
  • Don’t skip error handling: Your original code didn’t account for network errors or broken JSON responses. Adding those checks will save you headaches down the line.

内容的提问来源于stack exchange,提问作者Nicolas T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:39:34