Skip to main content

User-restricted RESTful APIs

Learn how to integrate your software with our OAuth 2.0 authorisation service for RESTful APIs

Overview

This page exaplins how to integrate your software with our OAuth 2.0 authorisation service for RESTful APIs.

In particular, it describes the combined authentication and authorisation pattern which is summarised in user-restricted APIs.

Note: in the steps below, make sure you use the domain name that's appropriate to the environment you're using:

Environment Domain name
Integration test int.api.service.nhs.uk
Production api.service.nhs.uk

 

1. Create an application

To access a user-restricted RESTful API, you need to create an application. This gives you access to your API Key and Secret, and allows you to specify your Callback URL, all of which you will need later in the process.

  1. If you don't already have one, create a developer account.
  2. Navigate to my developer account and sign in.
  3. Select 'My applications'.
  4. Select '+ NEW APP'.
  5. Enter details for your application.
  6. In the 'APIs' section, find the testing APIs you want to test with and activate them by clicking the slider.
  7. Enter the Callback URL for your application. See step 4 for more details.
  8. Select 'CREATE' to create the application.
  9. Make a note of the API Key and Secret.

2. Trigger the authorisation journey

When you want to start authorisation, your software should launch a browser window, or redirect the current browser window to our OAuth 2.0 authorisation endpoint:

https://<domain e.g. api.service.nhs.uk for production>/oauth2/v1/authorize

You need to include a number of parameters to make it work:

  1. ​client_id = <your application's API Key from step 1>
  2. ​​redirect_uri = <your application's Callback URL step 1, URL encoded>
  3. response_type = code
  4. state = <generated string>

The state parameter is something your software should generate randomly and the store for later - you use it in step 4 to recognise a valid redirect.

Here's an example redirect:

GET https://api.service.nhs.uk/oauth2/v1/authorize?
client_id=ZzOpF0ShQmMXGm8LSCI2HXyZh58GaD28&
redirect_uri=https%3A%2F%2Fwww.example.com%2Fcallback&
response_type=code&
state=30de877c-ee2f-15db-8314-0800200c9a66

3. User completes authentication and authorisation

Using the browser session your software triggered in step 2, the end user signs in to their NHS Identity account, using their NHS smartcard or a more modern alternative.

If they are using an NHS smartcard, they will need the following:

  • a smartcard reader attached to their device
  • the Identity Agent software installed on their device
  • the NHS Identity Hub software installed on their device

For more details on using smartcards, please contact us.

If testing with our username / password authentication service, use the following details:

  • Username: 910000000001
  • Password: Password1

You also need to associate a role with the user for the session, based on the roles that have been set up for them by their Registration Authority.

You will need to know the user role ID when you call the user-restricted API in step 6 below.

For more details on user role IDs, please contact us.

Normally with OAuth 2.0, the end user is then required to grant authority to their software to act on their behalf. However, for health professionals using NHS APIs, we do not require the end user to explicitly grant authority, so all the end user needs to do is sign in.

4. Receive authorisation results

Once the end user has completed authorisation, our authorisation server will redirect the user's browser back to your software.

You need to provide a callback endpoint on your web server for this. If your software don't have a web server, you will need to create one. For alternative approaches, please contact us.

You need to tell us the location of your callback endpoint so we know where to redirect back to. This is the Callback URL you specified in step 1 above.

Your callback endpoint will need to expect the following query parameters:

  • code = <authorization code, which you will need in the next step>
  • state = <the value of the state parameter you passed to our authorisation endpoint>​

We return state back to you so you can be sure the callback has come from us. For security reasons you should check the state we returned is the same as the state you sent.

Here's an example redirect:

GET https://www.example.com/callback?
code=uASW2e898xBrf_Y8047EnYIX3Sc&
state=30de877c-ee2f-15db-8314-0800200c9a66

For details on error conditions, please contact us.

5. Exchange authorisation code for access token

Once you have an authorisation code, your software needs to call our token endpoint to exchange it for an access token. This is an HTTP POST to the following endpoint:

https://<domain e.g. api.service.nhs.uk for production>//oauth2/v1/token

You need to include the following query parameters:

  • grant_type = authorization_code
  • code = <authorization code, as received in step 4>
  • redirect_uri = <your application's Callback URL, URL encoded>
  • client_id = <your application's API Key>
  • client_secret = <your application's Secret​>

Here's a complete example, as a CURL command:

curl -X POST -H "content-type:application/x-www-form-urlencoded" --data \
"client_secret=[YOUR-CLIENT-SECRET]\
&client_id=[YOUR-CLIENT-ID]\
&grant_type=authorization_code\
&redirect_uri=[YOUR-REDIRECT-URI]\
&code=[AUTHORIZATION-CODE]" \
https://api.service.nhs.uk//oauth2/v1/token

You will receive a response with a JSON response body, containing the following fields:

  • access_token = the access token you use when calling our user-restricted APIs
  • expires_in = the time after which the access token will expire, in seconds
  • id_token = an Open ID Connect ID token for the end user, which you must include when calling our user-restricited APIs
  • refresh_token = a token for refreshing the access token once it has expired (see step 7 below)
  • token_type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yxnwuR-sE1PA8anQhAPXvGoazlgd_08i51UfEhDk2IVWbGQHW8ZodACbbv51cubtz9oD8VvqT_wIEB0hNfSuFaU3A4lKCOmdm6QrPBK_ax2MHxtfPc0YIwGdtEJJcqyaE0mG9qpM6Z0VgzhkprMSbihKZwQdHsEn1ps1Xgn_UHnDAMbbJqb8F6r9zQgA',
 'refresh_token': 'r817EpAJ3Z3bq2lIDEH7Hey90VTaKhIS',
 'token_type': 'Bearer'}

For details on error conditions, please contact us.

6. Call a user-restricted API

Once you have an access token, you can call a user-restricted API.

You need to include the following headers in your call:

  • Authorization = Bearer <your access token from step 5>
  • NHSD-Identity-JWT = <user ID from step 5>
  • NHSD-Session-URID  = <user role ID from step 3>

Here's an example, using a CURL command:

curl -X GET https://sandbox.api.service.nhs.uk/hello/user \
-H "Authorization: Bearer [your access token from step 5]" \
-H "NHSD-Identity-JWT: [user ID from step 5]" \
-H "NHSD-Session-URID: [user role ID from step 3]"

For details of error conditions, please contact us.

7. Refresh token

Access tokens expire after a period of time and must be refreshed.

For details on how to refresh an access token, please contact us.

Requesting a new token

For details on how to request a new token once your token has expired, please contact us.

Revoking authority

For details on how to revoke an access token, please contact us.

Last edited: 6 April 2020 10:30 am