200-901 Understanding and Using APIs Practice Question
A developer is writing a Python script that uses the requests library to call a REST API. The API requires an API key in the header 'X-API-Key'. The developer wants to ensure the key is not hardcoded in the script and is instead read from an environment variable. Which code snippet correctly implements this?
⚠ Common exam trap
The trap here is assuming that any header can be used for an API key, but the API specifically requires the 'X-API-Key' header, not Authorization or query parameters.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
import os import requests api_key = os.environ['API_KEY'] headers = {'X-API-Key': api_key} response = requests.get('https://api.example.com/data', headers=headers)
The correct approach reads the API key from an environment variable and includes it in the 'X-API-Key' header as required. Using os.environ or os.getenv is secure. Passing the key as a query parameter, in Basic Auth, or in the Authorization header does not meet the API's authentication scheme.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
import os import requests api_key = os.environ['API_KEY'] headers = {'X-API-Key': api_key} response = requests.get('https://api.example.com/data', headers=headers)
Why this is correct
This snippet reads the API key from the environment variable 'API_KEY' using os.environ, then passes it in the headers dictionary to requests.get. This avoids hardcoding the key and follows security best practices. It correctly sets the custom header 'X-API-Key' as required by the API.
- ✗
import os import requests api_key = os.getenv('API_KEY') headers = {'Authorization': api_key} response = requests.get('https://api.example.com/data', headers=headers)
Why it's wrong here
This snippet sets the 'Authorization' header to the raw API key, but the API requires the key in the 'X-API-Key' header. The 'Authorization' header is typically used for Bearer tokens or Basic Auth, so the API would not recognize this format and would reject the request.
- ✗
import os import requests api_key = os.environ['API_KEY'] response = requests.get('https://api.example.com/data', params={'X-API-Key': api_key})
Why it's wrong here
This snippet passes the API key as a query parameter instead of a header. The API expects the key in the 'X-API-Key' header, so sending it as a query parameter would not authenticate the request. Query parameters are visible in URLs and logs, which is also less secure.
- ✗
import os import requests api_key = os.getenv('API_KEY') response = requests.get('https://api.example.com/data', auth=(api_key, ''))
Why it's wrong here
This snippet uses HTTP Basic Authentication with the API key as the username and an empty password. However, the API requires the key in the 'X-API-Key' header, not in Basic Auth. Using auth would send the key in the Authorization header, which the API would not recognize, leading to authentication failure.
Go deeper
Related to this question
About these practice questions
One of 975 original 200-901 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Cisco exam blueprint
This 200-901 practice question is part of Courseiva's free Cisco certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 200-901 exam.