Junior — Middle
How have you integrated with external APIs or services in your projects?
sobes.tech AI
Answer from AI
To integrate with external APIs in Python, I usually use the requests library, which allows sending HTTP requests (GET, POST, etc.) and receiving responses in a convenient format.
Main steps:
- Study the API documentation to understand which endpoints are available, what parameters and headers are needed.
- Formulate the request with the required parameters and headers.
- Send the request and process the response (usually JSON).
- Handle errors and exceptions (e.g., timeouts, error codes).
Example of a simple API request:
import requests
url = 'https://api.example.com/data'
params = {'key': 'value'}
headers = {'Authorization': 'Bearer YOUR_TOKEN'}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
print(data)
else:
print(f'Error: {response.status_code}')
For more complex integrations, I can use libraries for asynchronous requests (aiohttp), handle pagination, cache responses, or use SDKs if provided by the service.