JWT

A JSON Web Token (JWT) is a JSON based way of securely transferring information between two parties. To make a request that uses JWT, we first generate a token and then use it to make the request. To make this easy, we will use two popular npm libraries - jsonwebtoken and request. You have to list them in the manifest.json with the corresponding versions as shown below:

manifest.json

Copied Copy
1
2
3
4
"dependencies": [ "jsonwebtoken": "8.1.1", "request": "2.72.0" ]

The JWT token conists of three parts - a header, a payload, and a signature. The token (with the header, payload, signature) is generated by signing the payload with the secret or private key. It is then passed in the Authorization header of the request as shown in the sample below.

server.js

Copied Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var jwt = require('jsonwebtoken'); var request = require('request'); function jwt_request(){ // { foo: 'bar' } is the data and secret is the secret or private key var jwt_token = jwt.sign({ foo: 'bar' }, 'secret', { expiresIn: '1h' }); var options = { url: 'https://url.com', method: "POST", headers: { 'Authorization': jwt_token } }; request(options, function(error, response, body){ //logic to handle success or failure }); }
EXPAND ↓

Log in with your Freshdesk account

Enter your helpdesk URL to proceed to login

Proceed

By clicking "Proceed", you agree to our Terms of Use.