ICD API Code example

In order to be able to use the ICD APIs you need to follow these steps:

  • Register to our access management portal (https://icdaccessmanagement.who.int) and follow the instructions
  • The next step is getting an OAUTH 2.0 token from the server. You will make a request to the token endpoint with the following information.
    • Use Basic Authentication with your client Id and Secret
    • grant_type = client_credentials
    • scope = icdapi_access
    • Our token endpoint: https://icdaccessmanagement.who.int/connect/token
  • Then the server returns back a token which you add to your future requests to the ICD API. Please note that the tokens are valid for about 1 hour and after that one has to get a new one.

PHP example

Below you will find and example in PHP using the Client URL Library

    						
$tokenEndpoint = "https://icdaccessmanagement.who.int/connect/token";
$clientId = "..."; //of course not a good idea to put id and secret in the source code
$clientSecret = "..."; //you could read from an encyrpted source in the production
$scope = "icdapi_access";
$grant_type = "client_credentials";


// create curl resource to get the OAUTH2 token
$ch = curl_init();

// set URL to fetch
curl_setopt($ch, CURLOPT_URL, $tokenEndpoint);

// set HTTP POST
curl_setopt($ch, CURLOPT_POST, TRUE);

// set data to post
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
			'client_id' => $clientId,
			'client_secret' => $clientSecret,
			'scope' => $scope,
			'grant_type' => $grant_type
));

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$result = curl_exec($ch);
$json_array = (json_decode($result, true));
$token = $json_array['access_token'];

// close curl resource
curl_close($ch);



// create curl resource to access ICD API
$ch = curl_init();

// set URL to fetch
curl_setopt($ch, CURLOPT_URL, 'https://id.who.int/icd/entity');

// HTTP header fields to set
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Authorization: Bearer '.$token,
			'Accept: application/json',
			'Accept-Language: en'
));

// grab URL and pass it to the browser
curl_exec($ch);

// close curl resource
curl_close($ch);  							
							
						

Python example

Below you will find and example in Python using the Requests library

    						
import requests

token_endpoint = 'https://icdaccessmanagement.who.int/connect/token'
client_id = '...'
client_secret = '...'
scope = 'icdapi_access'
grant_type = 'client_credentials'


# get the OAUTH2 token

# set data to post
payload = {'client_id': client_id, 
	   	   'client_secret': client_secret, 
           'scope': scope, 
           'grant_type': grant_type}
           
# make request
r = requests.post(token_endpoint, data=payload, verify=False).json()
token = r['access_token']


# access ICD API

uri = 'https://id.who.int/icd/entity'

# HTTP header fields to set
headers = {'Authorization':  'Bearer '+token, 
           'Accept': 'application/json', 
           'Accept-Language': 'en'}
           
# make request           
r = requests.get(uri, headers=headers, verify=False)

# print the result
print (r.text)