0

I need to access Sparkpost data using their API. They do so via Authentication type = 'API key' instead of Basic Auth. I can do Basic Auth in Python using the code below.

import requests
import json
import requests
from requests.auth import HTTPBasicAuth
Response_API = requests.get('https://api.sparkpost.com/api/v1/metrics/sending-domain?from=2023年01月09日T08:00&metrics=count_sent', auth = HTTPBasicAuth('key', 'abcd1234xyz_key'))
Data = Response_API.text
print(Data)

I know I can't use this piece of code to get the data from API using 'API key' type. can someone please tell me how to do this?

asked Jan 10, 2023 at 19:01

1 Answer 1

1

You can pass it in headers:

headers = {
 'Accept': 'application/json',
 'x-api-key': API_KEY
}
res = requests.get('https://api.sparkpost.com/api/v1/metrics/sending-domain?from=2023年01月09日T08:00&metrics=count_sent', headers=headers)
print(res.text)
answered Jan 10, 2023 at 19:10
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, by API_KEY - do you mean the actual key? by the way - either of it didn't work. It gives me NameError
API_KEY is just a variable that holds the key, try just api_key instead of x-api-key
hmm I got you. I did all the changes and now I see error - Unauthorized. so it still didn't work
Looks like the api key isn't valid
hey - got to know what changes need to be done. You're all right. Instead of x-api-key I had to write Authorization and it works now. not your fault - that keyword is app specific, so for Sparkpost I need to write Authorization. thank you again for your help

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.