Search code examples
javascriptgetes6-promise

Javascript REST API - You must specify an API key to make request


I get a 403 "You must specify an API key to make request" when trying to get data from a 3rd party API (Klaviyo).

const { id } = req.body

request.get({
    url: `https://a.klaviyo.com/api/v1/person/${id}`,
    headers: {
        api_key: process.env.KLAVIYO_API_KEY
    }
}, (error, response, body) => {
    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, response: response, err: error })
    }
})

I've also tried with:

headers: {"Authorization": [API_KEY]}

data: {api_key: [API_KEY]}

Solution:

const { id } = req.body

request.get({
    url: `https://a.klaviyo.com/api/v1/person/${id}`,
    qs: {
        api_key: process.env.KLAVIYO_API_KEY
    }
}, (error, response, body) => {
    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, response: response, err: error })
    }
})

Solution

  • Short answer: add it under params.api_key (as part of the GET request).

    From the klaviyo documentation:
    "You authenticate to the People API by providing one of your private API keys as part of each request. (...) Authentication happens via the api_key parameter in each request. It can be sent as part of the GET or POST parameters."