API key authentication - Java tutorial
Connect to an application-restricted REST API using API key authentication and the Java programming language.
Overview
This tutorial shows you how to connect to an application-restricted REST API using API key authentication and the Java 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 Java 17 and maven 3.8.6, so you need to have these installed.
Check out the GitHub repository
You can find the code for this Java application-restricted REST API - API key authentication tutorial in our GitHub repository
This project contains:
- a HelloWorld class - the methods of this class make an application-restricted request to the API endpoint
- the App class - this contains the main entry point to run the program
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 |
---|---|
API_KEY | 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.
source .env
java -jar target/hello-world-auth-example-1.0-SNAPSHOT-jar-with-dependencies.jar
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:33 pm