Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access API data using authentication type 'API key' in Python?

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-09T08: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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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-09T08:00&metrics=count_sent', headers=headers)
print(res.text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading