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.

By default, Apitally doesn’t capture any sensitive information, making it suitable for applications with strict privacy requirements. If you need more detailed insights, request logging is available as an opt-in feature that you can configure to match your specific requirements.

Getting started

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

  1. Get a client ID by creating an app in the Apitally dashboard.

  2. Add the apitally package to your project’s dependencies:

npm install apitally
  1. Configure Apitally by running the below Ace command. You’ll be prompted to enter the client ID for your app:
node ace configure apitally/adonisjs

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. To capture validation and server errors, modify your exception handler in app/exceptions/handler.ts:
import { ExceptionHandler, HttpContext } from '@adonisjs/core/http'
import { captureError } from 'apitally/adonisjs'

export default class HttpExceptionHandler extends ExceptionHandler {
  async report(error: unknown, ctx: HttpContext) {
    captureError(error, ctx)
    return super.report(error, ctx)
  }
}

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.

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

export default class ApitallyConsumerMiddleware {
  async handle(ctx: HttpContext, next: NextFn) {
    if (ctx.auth.isAuthenticated) {
      setConsumer(ctx, 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!