Apilytics is a service that lets you analyze operational, performance and security metrics from your APIs easily.
Make sure to check out our out-of-the-box middleware packages first:
-
Sign up and get your API key from https://apilytics.io - we offer a completely free trial with no credit card required!
-
Install this package:
yarn add @apilytics/core
# OR
npm install @apilytics/core
- Set your api key and create a middleware which measures the execution time and sends the metrics:
A good practice is to securely store the API key as an environment variable. You can leave the env variable unset in e.g. development and test environments, and make the middleware be disabled if the key isundefined
.
my-apilytics-middleware.js
:
import { milliSecondTimer, sendApilyticsMetrics } from '@apilytics/core';
const myApilyticsMiddleware = async (req, handler) => {
const apiKey = process.env.APILYTICS_API_KEY;
if (!apiKey) {
return handler(req);
}
const timer = milliSecondTimer();
const res = await handler(req);
sendApilyticsMetrics({
apiKey,
path: req.path,
query: req.queryString,
method: req.method,
statusCode: res.statusCode,
requestSize: req.bodyBytes.length,
responseSize: res.bodyBytes.length,
userAgent: req.headers['user-agent'],
ip: req.headers['x-forwarded-for']?.split(',')[0].trim(),
timeMillis: timer(),
});
return res;
};
- No. The middleware does all of its requests to the Apilytics API in the background, by using promises without awaiting them, so it will not slow down your normal request handling.
- None :)
@apilytics/core
is tested to work on all the currently supported versions of Node: 12, 14, 16, and 17.