# The sample Python script below sends a request to the Migration Data Portal API to retrieve data for ‘stock_abs_’ and 'refug_host' indicators in 2015 and 2020. It will raise an exception if the request is unsuccessful. Otherwise, it parses the JSON response from the API.
# importing necessary libraries.
import requests
# Setting up the API URL.
base_url = 'https://www.migrationdataportal.org/api/international-values?'
# Setting up the request header with API key.
header_ = {'api-key': '3827056fe2ed0c23fc3a643336e30000'}
# Defining the parameters for the GET request.
params_ = {
# Specifying the indicators.
'indicator': 'stock_abs_+refug_host',
# Specifying the years for which data is requested.
'year': '2015+2020'}
# Handling exceptions and errors.
try:
# Making a GET request with the specified header and parameters.
api_response = requests.get(base_url, headers=header_, params=params_)
# Raise an exception if the request returned an unsuccessful status code (e.g., 400, 401, 404, etc.).
api_response.raise_for_status()
# Parsing the JSON response if the request was successful.
response_json = api_response.json()
except Exception as e:
# Catch any Exception and print it.
print("An error occurred:", e)