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 package.json
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 should be added after any logging middleware and before other middleware or application code. Request Flow │ ┌───────────────────────────────────┐ │ │ Logging Middleware │ │ └───────────────────────────────────┘ │ ┌───────────────────────────────────┐ │ │ Api Protect Middleware │ │ └───────────────────────────────────┘ │ ┌───────────────────────────────────┐ │ │ Other Middleware │ │ └───────────────────────────────────┘ │ │ ▼
Example using Express middleware
// example using Express middleware const express = require('express') const apiprotect_middleware = require('./apiprotect-express-middlware.js') const app = express() app.use(apiprotect_middleware())
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.
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 client_id to Express middleware
// example passing client_id to Express middleware const express = require('express') const apiprotect_middleware = require('./apiprotect-express-middlware.js') const settings = require('settings') const app = express() app.use(apiprotect_middleware({"clientId": settings.clientId}))
Step 4: Start the agent
The NodeJS middleware communicate with an agent process running in the same VPC via HTTP. The agent binary should be executed along with the NodeJS application, and it must be reachable via HTTP from the NodeJS application.