UNPKG

8.33 kBMarkdownView Raw
1<img src="https://dadi.cloud/assets/products/dadi-api-full.png" alt="DADI API" height="65"/>
2
3[![npm (scoped)](https://img.shields.io/npm/v/@dadi/api.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/api)
4[![coverage](https://img.shields.io/badge/coverage-89%25-yellow.svg?style=flat)](https://github.com/dadi/api)
5[![Build Status](https://travis-ci.org/dadi/api.svg?branch=master)](https://travis-ci.org/dadi/api)
6[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
7
8## DADI API
9
10* [Overview](#overview)
11* [Requirements](#requirements)
12* [Your First API Project](#your-first-api-project)
13* [Links](#links)
14
15## Overview
16
17DADI API is built on Node.JS. It is a high performance RESTful API layer designed in support of API-first development and the principle of COPE. It can use virtually any database engine, such as [MongoDB](https://github.com/dadi/api-mongodb), [CouchDB](https://github.com/dadi/api-couchdb), [RethinkDB](https://github.com/dadi/api-rethinkdb) or simply a [JSON filestore](https://github.com/dadi/api-filestore).
18
19You can consider it as the data layer within a platform (including the data model). It is designed to be plugged into a templating layer (such as [DADI Web](https://dadi.cloud/en/web)), a mobile application or to be used with any other data consumer.
20
21Calls to a DADI API can contain your business/domain logic (the part of a platform that encodes the real-world business rules that determine how data is created, displayed, stored and changed). It has full support for searching, filtering, limiting, sorting, offsetting, input validation and data aggregation (through support for MongoDB's aggregation pipeline).
22
23It has built-in support for oAuth2, includes full collection-level ACL, can connect to multiple databases out of the box, provides native document versioning at collection level, supports static endpoints, includes automatic indexing, has a caching layer and can be run in a clustered configuration.
24
25DADI API provides a starting point that's further advanced than a framework. It allows you to get a complete data layer up and running in minutes.
26
27It is part of DADI, a suite of components covering the full development stack, built for performance and scale.
28
29## Requirements
30
31* **[Node.js](https://www.nodejs.org/)** (supported versions: 6.11.x, 8.9.x)
32* A [data connector module](https://www.npmjs.com/search?q=keywords:dadi-api-connector)
33
34## Your first API project
35
36### Install API
37
38The quickest way to get started with *API* is to use [DADI CLI](https://github.com/dadi/cli). See [Creating an API](https://docs.dadi.cloud/api#creating-an-api) for full installation details.
39
40
41### Configuration
42
43API starts with some sensible defaults, so it's not necessary to understand all the configuration options available when first running the application.
44
45Configuration is handled using JSON files specific to the application environment. For example in the production environment a file named `config.production.json` will be used. Configuration files must be placed in a `config` folder in your application root, for example `config/config.production.json`. The default start up environment is `development`, using the configuration file at `config/config.development.json`.
46
47The bare minimum required for running the API is a `server` block. With only a `server` block, default values are used for all other properties.
48
49**Sample configuration**
50
51```json
52{
53 "server": {
54 "host": "127.0.0.1",
55 "port": 3000
56 }
57}
58```
59
60### Start the server
61
62API can be started from the command line simply by issuing the following command:
63
64```bash
65$ npm start
66```
67
68#### Test the connection
69
70With the default configuration, our API server is available at http://localhost:3000. If you've modified the configuration file's `server` block, your API will be available at the address and port you've chosen. Use cURL to check the server is running, if the connection can be made you will receive the following "Unauthorised" message.
71
72```bash
73$ curl http://localhost:3000
74```
75
76```json
77{ "statusCode": 401 }
78```
79
80
81#### Check the response headers
82
83```bash
84$ curl -I http://localhost:3000
85```
86```json
87HTTP/1.1 401 Unauthorized
88content-type: application/json
89content-length: 18
90Date: Thu, 20 Apr 2017 23:42:25 GMT
91Connection: keep-alive
92```
93
94### Authentication
95
96The HTTP 401 response received in the previous step shows that the server is running. To start using the REST endpoints you'll need a user account so you can obtain access tokens for interacting with the API.
97
98User accounts provide an authentication layer for API. Each user account has a *__clientId__* and a *__secret__*. These are used to obtain access tokens for interacting with the API. See the [Authentication](https://docs.dadi.cloud/api#authentication) section of the API documentation for full details.
99
100#### Creating the first user
101
102[CLI](https://github.com/dadi/api) contains an interactive "Client Record Generator" to help you create user accounts. Run the following command in the directory where you installed *API*:
103
104```bash
105cd my-new-api
106dadi api clients:add
107```
108
109If you need to create user accounts in other environments (for example following a deployment to a live server), add the environment to the following command:
110
111```bash
112$ NODE_ENV=production npm explore @dadi/api -- npm run create-client
113```
114
115### Run API as a service
116To run your API application in the background as a service, install Forever and Forever Service:
117
118```bash
119$ npm install forever forever-service -g
120
121$ sudo forever-service install -s index.js -e "NODE_ENV=production" api --start
122```
123
124You can now interact with the `api` service using the following commands:
125
126```bash
127$ [sudo] service api start
128$ [sudo] service api stop
129$ [sudo] service api status
130$ [sudo] service api restart
131```
132
133> Note: the environment variable `NODE_ENV=production` must be set to the required configuration version matching the configuration files available in the `config` directory.
134
135## Tests
136
137To run the tests after cloning the repository, run the following command:
138
139```
140$ npm test
141```
142
143> NOTE: API installs version 4.0.1 of Mocha and uses this when calling `npm test`. If you have Mocha installed globally and want to simply run `mocha`, if using version 4 or above, add `--exit` to the command so it becomes `mocha --exit`
144
145
146## Links
147* [API Documentation](https://docs.dadi.cloud/api/)
148
149## Contributors
150
151DADI API is based on an original idea by Joseph Denne. It is developed and maintained by the engineering team at DADI ([https://dadi.cloud](https://dadi.cloud))
152
153* Adam K Dean <akd@dadi.co>
154* Arthur Mingard <am@dadi.co>
155* David Longworth <dl@dadi.co>
156* Eduardo Bouças <eb@dadi.co>
157* Francesco Iannuzzelli <fi@dadi.co>
158* James Lambie <jl@dadi.co>
159* Joe Wagner
160* Joseph Denne <jd@dadi.co>
161* Kevin Sowers <kevin.sowers223@gmail.com>
162* Robert Stanford <rs@dadi.co>
163* Viktor Fero <vf@dadi.co>
164
165## Licence
166
167DADI is a data centric development and delivery stack, built specifically in support of the principles of API first and COPE.
168
169Copyright notice<br />
170(C) 2018 DADI+ Limited <support@dadi.cloud><br />
171All rights reserved
172
173This product is part of DADI.<br />
174DADI is free software: you can redistribute it and/or modify
175it under the terms of the GNU General Public License as published by
176the Free Software Foundation, either version 3 of the License, or
177(at your option) any later version ("the GPL").
178
179**If you wish to use DADI outside the scope of the GPL, please
180contact us at info@dadi.co for details of alternative licence
181arrangements.**
182
183**This product may be distributed alongside other components
184available under different licences (which may not be GPL). See
185those components themselves, or the documentation accompanying
186them, to determine what licences are applicable.**
187
188DADI is distributed in the hope that it will be useful,
189but WITHOUT ANY WARRANTY; without even the implied warranty of
190MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
191GNU General Public License for more details.
192
193The GNU General Public License (GPL) is available at
194http://www.gnu.org/licenses/gpl-3.0.en.html.<br />
195A copy can be found in the file GPL.md distributed with
196these files.
197
198This copyright notice MUST APPEAR in all copies of the product!
199