NodeJS Library Quickstart

Configure Project

  1. First install the npm library in your project.
npm i @dapi-co/dapi-node
  1. Import Dapi's library in your code.
import DapiApp from '@dapi-co/dapi-node'
const DapiApp = require('@dapi-co/dapi-node')
  1. Create a Dapi app with your App Secret
import DapiApp from '@dapi-co/dapi-node'

const dapi = new DapiApp({
  appSecret: 'YOUR_APP_SECRET',
})
const DapiApp = require('@dapi-co/dapi-node')

const dapi = new DapiApp.default({
  appSecret: 'YOUR_APP_SECRET',
})
  1. Now you can use any of the functions of the DapiApp instance, dapi. Here is an example for getAccounts.
const DapiApp = require('@dapi-co/dapi-node')

const dapi = new DapiApp.default({
    appSecret: 'YOUR_APP_SECRET',
})

try {
    dapi.data.getAccounts('ACCESS_TOKEN', 'USER_SECRET')
        .then(accounts_response => {
            console.log(accounts_response)
            //do something with the response 
        })

} catch (error) {
    console.dir(error)
}

Configure SDK Server

👍

Pre-Conditions

To initialize an SDK Server you need to have your app registered in Dapi Dashboard. If you haven't already done, you can find guides on how to do it on following links:

  1. Follow the steps in Configure Project first in order to obtain and import the library.

  2. Now inside your endpoint, our code will basically use the request and result variables. We will assume you named them req and res

try {

  const dapiResponse = dapi.handleSDKDapiRequests(req.body, req.headers)
  res.send(dapiResponse)

} catch (error) {

  //Handle Network Errors
  console.dir(error)

}

That's it. You can now test Dapi in action

Complete SDK Server Example

Here is a complete example of how it will look like in a node express app. You can see an example for both ways of importing JavaScript modules.

var express = require('express')
const DapiApp = require('@dapi-co/dapi-node')
var cors = require('cors')

const app = express()
const port = 8060 // default port to listen
app.use(cors())
app.use(express.json())

const dapi = new DapiApp.default({
  appSecret: 'YOUR_APP_SECRET',
})

// define a route handler for the sdk
app.post('/dapi', async (req, res) => {

  try {
    
    const dapiResponse = await dapi.handleSDKDapiRequests(req.body, req.headers)
    res.send(dapiResponse)

  } catch (error) {

    //Handle Network Errors
    console.dir(error)
  }
})

// start the Express server
app.listen(port, () => {
  console.log(`server started at http://localhost:${port}`)
})
import * as express from 'express'
import DapiApp from '@dapi-co/dapi-node'

const app = express()
const port = 8060 // default port to listen
app.use(express.json())

const dapi = new DapiApp({
  appSecret: 'YOUR_APP_SECRET',
})

// define a route handler for the sdk
app.post('/dapi', async (req, res) => {

  try {

    const dapiResponse = await dapi.handleSDKDapiRequests(req.body, req.headers)
    res.send(dapiResponse)

  } catch (error) {

    //Handle Network Errors
    console.dir(error)
  }
})

// start the Express server
app.listen(port, () => {
  console.log(`server started at http://localhost:${port}`)
})