Installing DADI API
Niklas Iversen 2017-04-03T14:59:42Z
328 words
2 min read

Create a new API project

$ mkdir my-api-app
$ cd my-api-app

Initialise the project

Running npm init adds a file called package.json to your project, allowing you to easily add dependencies to it:

$ npm init

Install @dadi/api from NPM

All DADI platform microservices are available from NPM. To add API to your project as a dependency:

$ npm install --save @dadi/api

Add an entry point

You’ll need an entry point for your project. We’ll create a file called index.js and later we will start the application with node index.js.

$ touch index.js

Add the following to the new file:

 *  index.js
var app = require('@dadi/api')

app.start(function() {
  console.log('API Started')

Create the first user

User accounts are required to provide an authentication layer for API. Each user has a “clientId” and a “secret”. These are used to obtain access tokens for interacting with the API. See the Authentication section for full details.

$ npm explore @dadi/api -- npm run create-client

This will start the Client Record Generator, accessing you a series of questions and finally inserting the client record into the database you have configured.

To ensure the correct database is used for your environment, add an environment variable to the command:

$ NODE_ENV=production npm explore @dadi/api -- npm run create-client

Start the API server

The moment of truth!

$ node index.js

Running API in the background

To run API in the background, install forever and forever-service globally:

$ npm install -g forever forever-service

Install your new API as a service and ensure it loads on boot:

$ forever-service install -s index.js -e NODE_ENV=production api --start

Note the environment variable NODE_ENV must be set to target the required configuration settings. Read more about API configuration.

You can now interact with the service using the following commands:

$ [sudo] service api start
$ [sudo] service api stop
$ [sudo] service api status
$ [sudo] service api restart

And you’re done, now move on to API configuration.

See something to improve? Edit in Github.