I have been testing FX and scratch orgs with jsforce and I based my first tests with this example. https://jsforce.github.io/blog/posts/20170629-salesforce-dx-with-jsforce.html With the recommendation on the page, I tried using the JWT flow, but I am encountering auth issue with the created org. I just can't connect to it. I have followed https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_auth_connected_app.htm.

Am I missing something or there is an issue in DX beta?

Here is a snippet of code I put together to show the flow I do.

    const jwt = require('jsonwebtoken');
    const rp = require('request-promise');

    let options = {
      issuer: clientId,
      audience: audience,
      expiresIn: 20000,
      algorithm: 'RS256'

    let token = jwt.sign({prn: userName}, privateKey, options);

    const accessToken = await rp(
        method: 'POST',
        uri: 'https://login.salesforce.com/services/oauth2/token',
        form: {
            'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
            'assertion': token
    .then(response => {
        return reply.access_token;

    let loginUrl = "https://CS65.salesforce.com";
    const hubConn = new jsforce.Connection(
            instanceUrl: loginUrl,
            accessToken: accessToken,
            version: "40.0"

    const scratchOrgInfo = hubConn.sobject('scratchOrgInfo');
    await scratchOrgInfo.describe();

    // create scratch org
    console.log('Creating scratch org....');
    const {id, success, errors} = await scratchOrgInfo.create({
        Country: 'US',
        Edition: 'Developer',
        OrgName: 'testorg',
        AdminEmail: AdminEmail,
        ConnectedAppConsumerKey: clientId,
        ConnectedAppCallbackUrl: redirectUrl,
    if (!success) {
        throw new Error('Error occurred while creating scratch org');

    const orgInfo = await scratchOrgInfo.retrieve(id);

    // establish connection with scratch org
    const loginUrl = `https://${orgInfo.SignupInstance}.salesforce.com`;
    const scratchOrgConn = new jsforce.Connection(
            instanceUrl: loginUrl,
            accessToken: accessToken,
            version: "40.0"
    const scratchOrgIdentity = await scratchOrgConn.identity() //Fails here on calling https://{instanceUrl}.salesforce.com/services/data/v40.0

I am posting a sample code that works for me. I see that you are passing token information as formdata instead of body. and have you checked your response for token? does it return a valid value or is it blank?

var rsa_payload = {

var rsa_options = {
   expiresIn: '1h'

 // Sign the JWT payload
var assertion = jwt.sign(

var options = {
  url: reqUrl,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'accept': 'application/json'

// Make the OAuth call to generate a token
request.post(options, function(error, response, body) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.