Find out what NHS Digital is doing to support the response on our Coronavirus update page.
If you are a patient needing advice visit the coronavirus page on NHS.UK
This developer resource is in beta. We welcome your feedback to api.management@nhs.net.
Open-access REST API tutorial
This tutorial shows you how to connect to an open-access REST API.
Open-access REST APIs are easy to use. They don't require any kind of authorisation, you just need to make an HTTP request to the relevant API endpoint.
In this tutorial we'll show you how to do this using our example API, Hello World.
We have given you examples of these methods:
- using the 'Try this API' feature
- using Java
- using .NET (C#)
- using Javascript
- using Python
Using the 'Try this API' feature
You can call an open-access REST API directly from the Developer Hub using the 'Try this API' feature:
- In a new browser tab, navigate to our Hello World API.
- In the left-hand navigation, select
/hello/.... This will display the endpoints available in the API. - In the left-hand navigation, select
world. This is an example open-access endpoint. - In the main panel, read the API specification. Notice that there are no request parameters and that a successful response is a JSON object containing a single field
message. - In the right-hand panel (where it says 'Try this API'), click 'Execute'. This calls the endpoint and displays the response. If all is working well, the response should be
200 OKwith a suitable JSON object in the response body. - To see more details, click on the maximise icon to the right of 'Try this API'. You'll be able to see the request as a cURL command or as HTTP, and the response as raw HTTP.
Using Java
You can call an open-access REST API using your favourite programming language. For example, to call the API from a Java program:
- Launch your favourite Java code editor.
- Create a new class called
HelloWorld.java. - Enter the following code into the class file:
HelloWorld.java
Java
- Compile and run your Java program. If all is working well, the response should be
200 OKwith a suitable JSON object in the response body.
Using .NET (C#)
To call the API from a C# program:
-
Launch your favourite C# code editor.
-
Create a new file called
HelloWorld.cs. -
Enter the following code into the class file:
HelloWorld.cs
C#
- Compile and run your Java program. If all is working well, the response should be
200 OKwith a suitable JSON object in the response body.
Using Javascript
- Launch your favourite Javascript code editor.
- Create a new script called
HelloWorld.js. - Enter the following code into the script:
HelloWorld.js
JavaScript
- Run this script.
- If all is working well, the response should be
200 OKwith a suitable JSON object in the response body.
Using Python
- Launch your favourite Python code editor.
- Create a new script called
HelloWorld.py. - The following code snippet requires the external dependency requests. To install requests, simply run this command in your terminal:
$ pipenv install requests - Enter the following code into the script:
HelloWorld.py
Python
- Run this script.
- If all is working well, the response should be
200 OKwith a suitable JSON object in the response body.
Application-restricted REST API tutorial
This tutorial shows you how to connect to an application-restricted REST API.
To call an application-restricted REST API, you need to tell the API who the calling application is. You do this by including your application's API key when you call the API.
In this tutorial we'll show how to do this using our example API, Hello World.
Whichever method you use you’ll need to create an application first in order to generate an API key.
Creating an application
The following steps can be used to create an application. This will generate an API key which can be used in all the below methods. You must send the API key in the ‘apikey’ header when you call the API.
- In a new browser tab, navigate to My developer account.
- Sign in to your developer account by selecting 'Sign in' in the top navigation bar.
- View your applications by selecting 'My applications'.
- Create a new application by clicking the
'+NEW APP'button. - Give your new application a name, such as 'Hello World Example Application', and a description, such as 'Application for learning how to call APIs'.
- Activate the Hello World API for your application. To do this, further down the page, find the Hello World API and click the slider.
- Select 'Create' to create the application. Your new application details will be displayed.
- Make a note of your API key. You can copy it to your clipboard by clicking the ‘copy’ icon.
Using the 'Try this API' feature
You can call an application-restricted REST API directly from the Developer Hub using the 'Try this API' feature:
-
Create an application using the guide above.
-
In a new browser tab, navigate to our Hello World API.
- In the left-hand navigation, select
/hello/.... This will display the endpoints available in the API. - In the left-hand navigation, select
application. This is an example application-restricted endpoint. - In the main panel, read the API specification. Notice that there are no request parameters and that a successful response is a JSON object containing a single field
message. Also notice that the API requires a header calledapikey. - In the right-hand panel (where it says 'Try this API'), click 'Execute'. This calls the endpoint and displays the response. Because you haven't specified an API key, you should get a
401 Unauthorisedresponse. - Click the 'AUTHORIZE' button. You should see a pop-up box with authorisation options.
-
Under the heading 'Api Key Auth: api_key', where it says 'Please select app...', select your new application. If you don't see this option, make sure you are still signed in.
- Click 'AUTHORIZE', followed by 'OK'.
- Now click 'Execute' again in the right-hand panel. If all is working well, the response should be
200 OKwith a suitable JSON object in the response body. - To see more details, click on the maximise icon to the right of 'Try this API'. You'll be able to see the request as a cURL command or as HTTP, and the response as raw HTTP.
Using Java
To call the API from a Java program:
-
Create an application using the guide above.
-
Launch your favourite Java code editor.
-
Create a new class called
HelloApplication.java. -
Enter the following code into the class file:
HelloApplication.java
Java
- Replace
`YOUR-APIKEY`with your application’sAPI Key. - Compile and run your Java program. If all is working well, the response should be a suitable JSON object in the response body.
Using .NET (C#)
To call the API from a .NET application:
-
Create an application using the guide above.
-
Launch your favourite code editor.
-
Add the following code to your Class file:
HelloApplication.cs
C#
- Replace
`YOUR-APIKEY`with the obtained key. - Compile and run the code, the response should be a suitable JSON object in the response body.
Using Javascript
To call the API from a JavaScript application:
-
Create an application using the guide above.
-
Launch your favorite code editor.
-
Add the following code to your script:
HelloApplication.js
JavaScript
- Replace
`YOUR-APIKEY`with the obtained key. - Run the code in your browser, the response should be a suitable object logged into the browser console.
Using Python
To call the API from a Python script:
-
Create an application using the guide above.
-
Launch your favorite code editor.
-
Add dependency, using e.g.
`pip install requests`. -
Add the following code to your script:
HelloApplication.py
Python
- Replace
`YOUR-APIKEY`with the obtained key. - Run the script, the response should be a suitable JSON object in the response body.
User-restricted REST API tutorial
This tutorial shows you how to connect to a user-restricted REST API.
Details to follow.