Hi,
I’ve successfully completed the onboarding process, obtained an API key and can retrieve the limited amount of content available in the sandbox API.
I want to use the integration test AI to retrieve more content, however all of the requests return 401 unauthorised, even though they’re made with the API key.
What do I have to do in order to be able to retrieve this content?
Thanks,
Richard
Hi Richard,
Thanks for getting in touch.
What language are you using?
I can provide you with some sample code.
Please take a look at the ‘NHS Website Content API migration assistant’.
This may help.
https://developer.api.nhs.uk/support/migration/assistant
Hi Adrian,
Thanks for your quick reply.
Upon investigation, looks like I need to generate a JWT key to access the integration API and can’t simply use the API key created at the end of the onboarding process - it wasn’t immediately clear.
Is this correct?
I found the example code:
I’ll give it a go and get back to you if I have any further issues.
Thanks,
Richard
Hi Richard,
No, the ‘NHS Website Content API’ does not require a JWT.
Passing in the ‘apikey’ in the request header is all that you need to do.
Do you have a code snippet of the request you are trying to make and I can take a look for you?
Hi Adrian,
Thanks for the info.
Here’s a code snippet (edited for clarity, with api key removed):
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add(“Accept”, “application/json”);
httpClient.DefaultRequestHeaders.Add(“apikey”, “32-character-api-key-goes-here”);
await httpClient.GetStringAsync(“https://int.api.service.nhs.uk/nhs-website-content/conditions?page=1”);
Here’s my log output:
Failed to fetch conditions index: https://int.api.service.nhs.uk/nhs-website-content/conditions?page=1
System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
Does the API key generated in my developer account cover all APIs or do I need one specific to the integration API? If so, where do I generate it?
Also, the only API I have connected is this one (which is probably irrelevant):
Connected APIs
NHS App (External Development)
The NHS Website Content API is not in the list to choose from though.
Does it need to be manually connected somehow?
Thanks and best regards,
Richard
Hi Adrian,
OK, so now I’ve created an app specific for the Integration test environment and connected it to the correct API:
NHS Website Content API (Integration Testing Environment)
but when I execute the code snippet above, the API returns a 500 error.
Best regards,
Richard
Hi Adrian,
Got it working in Postman - will investigate and get back to you.
Thanks very much for all your help!
Best regards,
Richard
Ah, brilliant.
Glad you got it working in Postman.
Let me know if you need any further assistance.
1 Like
Hi Adrian,
The difference was that the API requires a trailing / on the URL.
Without it, the response http status code is 500 and the body is:
{
“fault”: {
“faultstring”: “Execution of javascript.SearchAndReplaceResponse failed with error: Javascript runtime error: "SyntaxError: Empty JSON string. (SearchAndReplaceResponse.js:243)"”,
“detail”: {
“errorcode”: “steps.javascript.ScriptExecutionFailed”
}
}
}
Best regards,
Richard
Hi Richard,
Yeah, we are looking in to that at the moment.
The safest bet is to make sure all requests end with a trailing forward slash.
Cheers
We have resolved the issue regarding a missing trailing forward slash. The endpoints will now work with (or without) it.
1 Like