Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Overview

The Data Theorem API Protect NodeJS SDK is a NodeJS library that provides protection for APIs written in Javascript. The SDK integrates with your NodeJS API using the API Protect middleware which supports most popular NodeJS frameworks. To Protect an API, all the API developer needs to do is add the API Protect middleware to their web application code set the client Id we provide

Installation

Step 1: Add the package to dependencies

To install API Protect the provided NodeJS package must be added as a dependency, typically by adding it to a package.json file. Here is an example of how to add the package:

Example adding dependency to via npm

npm install ./apiprotect-v1.0.0.tgz

Example adding dependency to via yarn

yarn add file:./apiprotect-v1.0.0.tgz

Step 2: Add the middleware to the application

The middleware for your web framework should be imported and added to your application.

Note: The ordering of middleware is important

This middleware may be added before or after any logging middleware,
but it should be after body parsing middleware and before other middleware
or application code.


                                        Request Flow
    ┌───────────────────────────────────┐   │
    │      Logging Middleware           │   │
    └───────────────────────────────────┘   │
    ┌───────────────────────────────────┐   │
    │      Body Parsing Middleware      │   │
    └───────────────────────────────────┘   │
    ┌───────────────────────────────────┐   │
    │      Api Protect Middleware       │   │
    └───────────────────────────────────┘   │
    ┌───────────────────────────────────┐   │
    │         Other Middleware          │   │
    └───────────────────────────────────┘   │
                                            │
                                            ▼ 
                     *  OR *
                      
                                       Request Flow

    ┌───────────────────────────────────┐   │
    │      Body Parsing Middleware      │   │
    └───────────────────────────────────┘   │                                       │
    ┌───────────────────────────────────┐   │
    │      Api Protect Middleware       │   │
    └───────────────────────────────────┘   │
    ┌───────────────────────────────────┐   │
    │      Logging Middleware           │   │
    └───────────────────────────────────┘   │
    ┌───────────────────────────────────┐   │
    │         Other Middleware          │   │
    └───────────────────────────────────┘   │
                                            │
                                            ▼                     
                      
                      
                      

Example using Express middleware

const express = require('express');
const apiprotect = require('apiprotect');

const app = express();

// Register body parsing middlware then APIProtect middlware
app.use(express.json());
app.use(apiprotect()); // ClientId read from ENV: `DT_API_PROTECT_CLIENT_ID`

Step 3: Set the Client ID

For each API you protect Data Theorem provides a unique identifier which authenticates the SDK when it communicates with our services. The client id we provide can be set in two way, either via an environment variable DT_API_PROTECT_CLIENT_ID or by passing it directly to the middleware.

The client id is in a file named client_id in the sdk bundle you downloaded.

Example setting the client id via ENV Var

> source ./client_id && npm run

Setting the environment variable is the preferred way, but if you need to pass it to the middleware, here is how to do that:

Example passing clientId to Express middleware

const express = require('express');
const apiprotect = require('apiprotect');
const settings = require('settings')l

const app = express();

// Register body parsing middlware then APIProtect middlware
app.use(express.json());
app.use(apiprotect(settings.clientId)); // ClientId passed as param

  • No labels