Search code examples
javascripttypescripthttphttp-status-codes

How to get status code from response API without Http Get using KlaviyoClient?


I am using a third party API and want to get the status code response from this. It doesn't use the usual http.get(request, header) format.

How would we get the status code from this? This is the link I referenced

// Get the timeline for all events on a profile.
KlaviyoClient.profiles.getProfileMetricsTimeline({
    profileId: 'myProfileId',
    since: 1606262400,
    count: 50,
    sort: 'asc'
});

I'm using typescript and I tried to do

const response = await KlaviyoClient.profiles.getProfileMetricsTimeline({
    profileId: 'myProfileId',
    since: 1606262400,
    count: 50,
    sort: 'asc'
});

But this doesn't return the status code. What do we need to do? Thank you!


Solution

  • The KlaviyoClient library abstracts the handling of success and error responses.

    statusCode is only available on Error objects thrown from requests through the client library.

    These errors can be handled as follow

    let response;
    try {
       response = await KlaviyoClient.profiles.getProfileMetricsTimeline({
        profileId: 'myProfileId',
        since: 1606262400,
        count: 50,
        sort: 'asc'
       });
    } catch (e) {
       console.error(e.statusCode);
    }