Skip to main content

I am upgrading my apex code in salesforce for the new version of Klaviyo Api. But when I am trying to run the code authorisation is failing for some reasons. When I use this syntax:  req.setHeader('Authorization', 'Klaviyo-API-Key “My Api Key Hardcoded”');,

I am getting the response but i dont want hardcoded value to give here. while trying this syntax i am getting the error 400 :

 req.setHeader('Authorization','klaviyo_api_key '+ defaults.ApiKey__c);.

Can someone please give me a solution to this.Its urgent.

Hey @coder, can you provide a full error response here, as well as more details on the exact request you’re making? Please do not include your actual private api key. 

A 400 error indicates there’s likely an issue with the way your request payload is structured vs. there being an auth error. I’d expect an auth-related error to manifest with a 401
 


Hi @coder,

Just wanted to check if you are using the right case here for the prefix to be added in the req.setHeader().

I could see that the syntax you have given here has the prefix incorrect.

 req.setHeader('Authorization','klaviyo_api_key '+ defaults.ApiKey__c);.

The right prefix could be Klaviyo-API-Key

 req.setHeader('Authorization','Klaviyo-API-Key '+ defaults.ApiKey__c);.

Can you try this and let know if this works ?

 

Also as @Kim Strauch pointed out you can log the error and see the message since this is 400.

 

Thanks.