Apitally now supports AdonisJS

Introducing our new integration for AdonisJS, offering comprehensive API insights and request logging with minimal effort.

Simon GurckeSimon Gurcke//3 min read

We’re excited to announce that Apitally now supports AdonisJS.

AdonisJS is a full-featured, TypeScript-first Node.js framework designed for building scalable and maintainable web applications and APIs. What sets it apart from other frameworks is its deep focus on developer productivity, offering a convention-over-configuration approach similar to Laravel. It even has its own CLI tool and VSCode extension!

Apitally in a nutshell

Aligning with AdonisJS’s focus on developer productivity, Apitally brings powerful API monitoring, analytics and request logging to your projects with just a few lines of code:

  • Understand API usage and adoption
  • Track client and server errors
  • Monitor API performance
  • Log, find and inspect individual API requests and responses
  • Get notified when something goes wrong using synthetic health checks, heartbeats and custom alerts

Apitally provides intuitive dashboards for your APIs out of the box, so you don’t have to spend time building your own. The dashboards give you a clear overview of what’s happening with your API, while allowing you to drill down into any area of interest.

Screenshots of Apitally dashboard
API monitoring dashboards in Apitally

How it works

Apitally’s open-source SDK integrates with AdonisJS using a lightweight middleware which automatically captures key API metrics for each request and response. The middleware times the invocation of your route handlers and collects metadata about requests and responses, including method, path, status code, and validation errors.

The SDK aggregates this data client-side and sends it to the Apitally dashboard in the background at regular intervals, ensuring minimal impact on your application’s performance.

You can limit the data included in request logs, configure masking for sensitive information, and exclude requests from logging. The SDK applies masking and exclusions in your application before sending log data to Apitally.

Getting started

Setting up Apitally for your AdonisJS project only takes a few minutes.

  1. Get a write token by creating an app in the Apitally dashboard.

  2. Install and configure Apitally by running the following Ace command from your application directory. You’ll be prompted for your write token, environment, and capture settings:

node ace add apitally

This command will automatically:

  • Create a config file at config/apitally.ts
  • Register the Apitally provider in adonisrc.ts
  • Add the Apitally middleware to start/kernel.ts
  • Add environment variables to .env and start/env.ts
  1. Check that your exception handler in app/exceptions/handler.ts captures validation and server errors after handling them:
import { ExceptionHandler, HttpContext } from '@adonisjs/core/http'
import { captureException } from 'apitally/adonisjs'

export default class HttpExceptionHandler extends ExceptionHandler {
  async handle(error: unknown, ctx: HttpContext) {
    const result = await super.handle(error, ctx)
    captureException(error, ctx)
    return result
  }
}

After deploying your application, you will see data in the Apitally dashboard within a few seconds.

Identifying consumers

Apitally allows you to track API usage per consumer. To enable this, you can identify consumers in your application using the setConsumer function.

If your application uses authentication, it makes sense to set this based on the authenticated user. You could do this in a custom middleware, for example. Register this middleware after authentication and the Apitally middleware.

import { HttpContext } from '@adonisjs/core/http'
import { NextFn } from '@adonisjs/core/types/http'
import { setConsumer } from 'apitally'

export default class ApitallyConsumerMiddleware {
  async handle(ctx: HttpContext, next: NextFn) {
    if (ctx.auth.isAuthenticated) {
      setConsumer(ctx.auth.user!.email)
    }
    await next()
  }
}

Conclusion

With Apitally’s new AdonisJS integration, you can get insights into your API’s usage, errors, and performance with minimal effort. This allows you to stay productive and focus on building great APIs rather than setting up complex monitoring infrastructure.

For further instructions, including how to configure request logging, check out our full setup guide for AdonisJS.

Happy monitoring!