Skip to main content

API key authentication - C# tutorial

Connect to an application-restricted REST API using API key authentication and the C# programming language.

Overview

This tutorial shows you how to connect to an application-restricted REST API using 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 the Hello World API and 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 our GitHub repository

This project contains:

  • a HelloWorld class located in the HelloWorld.cs file. 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' of our 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 have your 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 dotnet cli 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 to create a developer account.

Last edited: 24 October 2022 4:37 pm