Node.js SDK Quick Links

Node.js Server-side SDK

verifyAndReadJwt(jwt, secret)

Verifies the signature of a JSON Web Token. If successful, it returns the instance id. You will use this for verifying and reading the JWT that was provided query string of the app component iframe. If you use the Client SDK to do the call to your server, the render JWT will be sent in the request headers.

Parameters:

Name Type Description
jwt String JSON Web Token you want to verify and read.
secret String The render secret, which can be found in the Developer Portal.

Example:

verifyAndReadJwt() example

const renderSecret = 'exampleRenderKey'

// request.get() is an Express method
const token = request.get('instance-jwt') || ''
const instanceId = sdk.verifyAndReadJwt(token, renderSecret)

generateJwt(instanceId, secret)

Generates a JSON Web Token with the instance id in the payload and a lifetime of one hour.

Parameters:

Name Type Description
instanceId String The instance id that was read from the render JWT.
secret String The API secret, which can be found in the Developer Portal.

Example:

generateJwt() example

const apiSecret = 'exampleApiKey'

// We use the instance id from the verifyAndReadJwt() example
const apiJwt = sdk.generateJwt(instanceId, apiSecret)

NOTE: Please refer to JWT.IO to find coding samples to generate a JWT outside of AppMarket SDK in different programming languages. You can also refer to this link to see one such example in groovy.

postContactAsync(jwt, contact)

Post contact.

Parameters:

Name Type Description
jwt String The API JWT.
contact Object The contact object.

Example:

postContactAsync() example

// We get the contact object from the request body. request.body is an Express property
const contact = request.body

// We use the apiJwt from the generateJwt() example
const result = await sdk.postContactAsync(apiJwt, contact)

getContactsAsync(jwt, queryString)

Get contacts.

Parameters:

Name Type Description
jwt String The API JWT.
queryString String A string containing all the query parameters.

Example:

getContactsAsync() example

const query = request.query // request.query is an Express property
const params = `scope=${query.scope}&offset=${query.offset}&limit=${query.limit}`

// We use the apiJwt from the generateJwt() example
const result = await sdk.getContactsAsync(apiJwt, params)

Node.js: Express example using SDK

Node.js Express example

import express from 'express'
import * as bodyParser from 'body-parser'
import * as sdk from 'appmarket-nodejs-sdk'

const app = express()
const port = 3000

const renderSecret = 'renderExampleKey'
const apiSecret = 'apiExampleKey'

app.use(bodyParser.json())

app.use((request, response, next) => {
  response.header('Access-Control-Allow-Origin', '*')
  response.header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, instance-jwt')
  next()
})

app.get('/contacts/get', async (request, response) => {
  const query = request.query
  const params = `scope=${query.scope}&offset=${query.offset}&limit=${query.limit}`

  const token = request.headers['instance-jwt'] || ''

  const instance = sdk.verifyAndReadJwt(token, renderSecret)
  const newJwt = sdk.generateJwt(instance, apiSecret)

  const result = await sdk.getContactsAsync(newJwt, params)
  if (result.status === 200) {
    response.send(await result.json())
  } else {
    response.send(`${result.status}: ${result.statusText}`)
  }
})

app.post('/contacts/post', async (request, response) => {
  const contact = request.body

  const token = request.get('instance-jwt') || ''

  const instance = sdk.verifyAndReadJwt(token, renderSecret)
  const newJwt = sdk.generateJwt(instance, apiSecret)

  const result = await sdk.postContactAsync(newJwt, contact)
  response.send(`${result.status}: ${result.statusText}`)
})

app.listen(port, (err) => {
  if (err) {
    return console.log('Something bad happened', err)
  }
  process.on('unhandledRejection', (reason, p) => {
    console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
  })
  console.log(`server is listening on ${port}`)
})