Creating a project
The Justice Data Platform uses a project-based system which will help keep your data assets secure, reproducible and scalable. For the first version of the Justice Data Platform, a project helps you organise and control access to models from the Justice AI Gateway.
When you create a project, you can:
- add members to it (optional)
- create an API key
- set the business unit it’s associated with
You can only add members who have an MOJ Microsoft account.
Once you have a working API key, you can use it to access AI models.
Check which endpoint you need to use
The Justice AI Gateway has two endpoints:
- an internal endpoint for users connecting from MOJ services
- a standard endpoint for users connecting from other environments
You should use the internal endpoint if you’re connecting from MOJ’s Cloud Platform. You can access it from https://internal.ai-gateway.justice.gov.uk.
Use the standard endpoint if you’re connecting from any other environment. You can access it from https://ai-gateway.justice.gov.uk.
Create an API key for your project
After creating your project, follow the steps on the Justice Data Platform to create an API key. Make sure you save the API key in a secure place.
If you need to make API key changes (for example, create a new key or regenerate an existing key), you can find more information in the Managing projects section of this user guide.
Do not use Justice AI Gateway API keys for AI-assisted coding tasks, such as code generation or completion. Request a GitHub Copilot licence for this instead.
Check your API key works
After you’ve created your API key, make a basic request to the Justice AI Gateway to check it works. You can do this by completing the steps below.
Test your API key in your IDE
Follow OpenAI’s documentation to install an official SDK for your language.
Set your API key as an environment variable using the following command:
export AI_GATEWAY_API_KEY="<your-api-key>"Send an API request in your IDE. You can use the Python example request below, replacing
<model-name>with the exact name of the model you want to use. You should replace the standard endpoint with the internal endpoint if you are connecting from MOJ’s Cloud Platform.import os from openai import OpenAI client = OpenAI( api_key=os.environ["AI_GATEWAY_API_KEY"], base_url="https://ai-gateway.justice.gov.uk/", ) response = client.responses.create( model="<model-name>", input="Reply with: Your API key is working", ) print(response.output_text)Check the response. If successful, you’ll receive
Your API key is workingor a similar response.
Test your API key with curl
If the request in your IDE is successful, you do not need to do this step. You can use it to help identify whether a problem is with your API key or the Justice Data Platform.
Set your API key as an environment variable using the following command:
export AI_GATEWAY_API_KEY="<your-api-key>"Send a request using
curl, replacing placeholders in the example below with your API key and the exact name of the model you want to use. You should also replace the standard endpoint with the internal endpoint if you are connecting from MOJ’s Cloud Platform.curl https://ai-gateway.justice.gov.uk/responses \ -H "Authorization: Bearer $AI_GATEWAY_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "<model-name>", "input": "Reply with: Your API key is working" }'Check the response. If successful, you’ll receive
Your API key is workingor a similar response.
Error handling for API calls
Your code should account for requests that fail and log information to help you identify and resolve issues.
Configure your error handling to log:
- the HTTP status code
- any error message
- the response body (if available)
- the time of the request
Do not log sensitive data such as API keys.
If the log does not make the cause clear, try the following:
- confirm your API key is correctly set as an environment variable
- check your request specifies the correct model name and includes the version number
- check the status page for any ongoing incidents
- make sure you have not exceeded usage limits (select your project in the Justice Data Platform, then go to Usage)
Raise an issue in the #ask-data-platform Slack channel if the problem continues, including your Project ID and Key ID (not your API key). You can find these by going to Projects in the Justice Data Platform and selecting your project.
How AI safeguards affect your requests
As part of our security policy, we use automated checks (‘guardrails’) to safeguard the Justice AI Gateway from malicious or harmful prompts.
If your API request is blocked by these checks, the API returns a 400 Bad Request response with a message referring to the AI Gateway security policy.
You should first try simplifying or rewording your prompt if it’s blocked.
If the problem continues, you can raise an issue in the #ask-data-platform Slack channel with the full error response. Our team can investigate platform issues, but the Justice AI Gateway does not retain prompt content, so we cannot determine why a specific prompt was blocked.