Nodejs samples for APIs integration

Hello everyone,

Anyone that would be familiar with the subject matter is in the group? Your advices would be appreciated.

Thanks!

Hi,
I’m not aware of any official nodejs API integration examples.
There is information on how to integrate with the APIs using our security and authorization patterns here as well as code examples for each security pattern in Java, C# and PHP Security and authorisation - NHS Digital. You should be able to adapt these examples to nodejs.

You would need to find out what security pattern(s) the API you are wanting to integrate with uses. - you should be able to find this out on the API catalogue - NHS Digital

Hello Mr Odunsi,

Thanks for the share.

For the application restricted RESTful JWT based solution, this should get you a signed JWT

const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
const fs = require('fs');

var payload={
  "iss": api_key,
  "sub": api_key,
  "aud": url+'/token',
  "jti": uuidv4()
}

var privateKey = fs.readFileSync(private_key);

var token = jwt.sign(
payload
,
privateKey,
{
	header:{
		kid:'test-1'
	},
	expiresIn:'5m',
 	algorithm: 'RS512'
 }); 

And then calling the oauth API should look a little like this to get your access_token

const auth_resp = await fetch('https://'+url+'/token', {
    method: 'POST',
    headers:{
      'Content-Type': 'application/x-www-form-urlencoded'
    },    
    body: new URLSearchParams({
    	grant_type : 'client_credentials',
		client_assertion_type : 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
		client_assertion : token
	})
})

console.dir(auth_resp.ok)
console.dir(auth_resp.status)

console.dir(...auth_resp.headers)
console.dir(await auth_resp.text())

Thank you ! This looks very helpful Mr. Chiverton. :+1: