API key authentication - C# tutorial - NHS Digital (2024)

Overview

This tutorial shows you how to connect to an application-restricted REST APIusing API key authentication and the C# programming language.

To call an application-restricted API, you need to tell the API which application is calling it.

When using API key authentication every request must contain a header which contains a key. This key is called an API key. There is no "standard" name for the header name and you should always refer to the documentation to check the correct name. In our tutorial, we send a request to theHello World APIand the API key header is called apikey.

Setting up your environment

This example project was developed using .NET version 6.0, so you need to have this installed.

Check out the GitHub repository

You can find the code for this C# application-restricted REST API - API key authentication tutorial in ourGitHub repository

This project contains:

  • a HelloWorldclass located in the HelloWorld.csfile. This class adds the API key as a header and sends a GET request to the specified endpoint

To follow this tutorial, download or clone this repository.

Create an application

You need to create an application using our Developer portal.

This gives you access to your application ID and API key. To do this, follow Step 1 'Create an application' ofour guide.

Notes:

  • when creating a new app, you need to select the 'Environment'. For this tutorial select 'Sandbox'.
  • when editing your application details and selecting the API you want to use, select 'Hello World (Sandbox)'. You might be prompted for a callback URL which is not required for the API key authentication method, so you can enter a dummy value such as https://www.example.com.
  • make a note of your API Key

Populate the project's environment variables

You should now haveyour application's API Key.

To run the example tutorial, you need to set the following environment variables.

Variable name Description
CLIENT_ID Your application's API Key
ENDPOINT The URL for the API you wish to call. In this tutorial, we make a request to the Hello World Sandbox's application-restricted endpoint:
https://sandbox.api.service.nhs.uk/hello-world/hello/application

You can set your environment variables in a file named .env. This project contains a sample env file to use:

  • rename env.sample to .env and modify it.
  • source it by running source .env

Run the code

Once you set the environment variables, you are ready to run the project.

Run the application

You should first source your environment variable file before executing your application.

Assuming you are using dotnetcli tool:

source .env

dotnet run

Run using Makefile

Alternatively you can set your environment variables in a file named .env. Then use the make command: make run. See the README for more info.

When you run the code, you should receive the following response from the Hello World application, showing you succeeded:

{ "message": "Hello Application!"}

Create a developer account

To get started with our tutorials and APIs, you need tocreate a developer account.

Last edited: 24 October 2022 4:37 pm

API key authentication - C# tutorial - NHS Digital (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6084

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.