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.

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

Example also available on the GitHub repository

    							
$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);  							
    							
    						

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

Example also available on the GitHub repository

    							
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)						
								
							

Below you will find and example in Java using the package org.json for parsing JSON response

Example also available on the GitHub repository

    							
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder; 
import javax.net.ssl.HttpsURLConnection;

import org.json.*;

public class ICDAPIclient {

	private final String TOKEN_ENPOINT = "https://icdaccessmanagement.who.int/connect/token";
	private final String CLIENT_ID = "...";
	private final String CLIENT_SECRET = "...";
	private final String SCOPE = "icdapi_access";
	private final String GRANT_TYPE = "client_credentials";


	public static void main(String[] args) throws Exception {

		String uri = "https://id.who.int/icd/entity";

		ICDAPIclient api = new ICDAPIclient();
		String token = api.getToken();
		System.out.println("URI Response JSON : \n" + api.getURI(token, uri));
	}


	// get the OAUTH2 token
	private String getToken() throws Exception {

		System.out.println("Getting token...");

		URL url = new URL(TOKEN_ENPOINT);
		HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
		con.setRequestMethod("POST");

		// set parameters to post
		String urlParameters =
        		"client_id=" + URLEncoder.encode(CLIENT_ID, "UTF-8") +
        		"&client_secret=" + URLEncoder.encode(CLIENT_SECRET, "UTF-8") +
			"&scope=" + URLEncoder.encode(SCOPE, "UTF-8") +
			"&grant_type=" + URLEncoder.encode(GRANT_TYPE, "UTF-8");
		con.setDoOutput(true);
		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
		wr.writeBytes(urlParameters);
		wr.flush();
		wr.close();

		// response
		int responseCode = con.getResponseCode();
		System.out.println("Token Response Code : " + responseCode + "\n");

		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();
		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();

		// parse JSON response
		JSONObject jsonObj = new JSONObject(response.toString());
		return jsonObj.getString("access_token");
	}


	// access ICD API
	private String getURI(String token, String uri) throws Exception {

		System.out.println("Getting URI...");

		URL url = new URL(uri);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		con.setRequestMethod("GET");

		// HTTP header fields to set
		con.setRequestProperty("Authorization", "Bearer "+token);
		con.setRequestProperty("Accept", "application/json");
		con.setRequestProperty("Accept-Language", "en");

		// response
		int responseCode = con.getResponseCode();
		System.out.println("URI Response Code : " + responseCode + "\n");

		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();
		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();

		return response.toString();

	}

}