Sample code for 30+ languages & platforms
React Native

Docusign JSON Web Token (JWT) Grant

See more DocuSign Examples

Demonstrates how to obtain an access token using the JSON Web Token (JWT) Grant. This is good for service integrations where authorization and authentication is automated and cannot have interactive Docusign account owner interaction. Consent for the access is obtained beforehand in various ways. See Obtaining Consent.

Chilkat React Native Downloads

React Native
import { Http, HttpRequest, HttpResponse, JsonObject, Jwt, PrivateKey } from '@chilkat/react-native'

async function chilkatExample() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // 1. Assume we have already requested and obtained application consent.
  //    (See Request Docusign Application Consent
  // 
  // 2. Create a JWT Token.
  // 
  const privKey = new PrivateKey();

  // Load an RSA private key from a PEM file.
  try {
    privKey.loadPemFile('qa_data/pem/docusign_private_rsa_key.pem');
  } catch {
    console.log(privKey.lastErrorText);
    return;
  }

  const jwt = new Jwt();

  // Build the JOSE header
  const jose = new JsonObject();
  // Use RS256.  Pass the string "RS384" or "RS512" to use RSA with SHA-384 or SHA-512.
  jose.appendString('alg', 'RS256');
  jose.appendString('typ', 'JWT');

  // Now build the JWT claims (also known as the payload)
  const claims = new JsonObject();

  // Replace these with actual values.
  // The client ID is also known as the "integration key" in Docusign.
  claims.appendString('iss', 'MY_DOCUSIGN_CLIENT_ID');
  // In your DocuSign Admin/Account/UserProfile, this is the API Username, such as 14612117-2530-4982-8c49-ba8766303272
  claims.appendString('sub', 'DOCUSIGN_USER_ID');
  claims.appendString('aud', 'account-d.docusign.com');
  claims.appendString('scope', 'signature');

  // Set the timestamp of when the JWT was created to now.
  const curDateTime = jwt.genNumericDate(0);
  claims.addIntAt(-1, 'iat', curDateTime);

  // Set the "not process before" timestamp to now.
  claims.addIntAt(-1, 'nbf', curDateTime);

  // Set the timestamp defining an expiration time (end time) for the token
  // to be now + 1 hour (3600 seconds)
  claims.addIntAt(-1, 'exp', curDateTime + 3600);

  // Produce the smallest possible JWT:
  jwt.autoCompact = true;

  // Create the JWT token.  This is where the RSA signature is created.
  const token = jwt.createJwtPk(jose.emit(), claims.emit(), privKey);

  console.log(token);

  // Do the following CURL statement to get the response JSON which contains the access token.
  // curl --data "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=YOUR_JSON_WEB_TOKEN" 
  //      --request POST https://account-d.docusign.com/oauth/token
  const http = new Http();

  const req = new HttpRequest();
  req.addParam('grant_type', 'urn:ietf:params:oauth:grant-type:jwt-bearer');
  req.addParam('assertion', token);

  req.httpVerb = 'POST';
  req.contentType = 'application/x-www-form-urlencoded';

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync('https://account-d.docusign.com/oauth/token', req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`response status = ${resp.statusCode}`);
  if (resp.statusCode !== 200) {
    console.log(resp.bodyStr);
    console.log('Failed.');
  } else {
    // Save the access token to a file for use in subsequent requests..
    // (Or you may simply persiste the access token in memory for your applicaton to use for subsequent REST API calls..)
    resp.saveBodyText(true, 'qa_data/tokens/docusign.json');
    console.log(resp.bodyStr);
    console.log('Success.');
  }
}