Skip to main content
This setup guide is for v1 of the JavaScript SDK (currently in beta).
This page guides you through the setup of the Apitally SDK for your Elysia application. If you don’t have an Apitally account yet, sign up before getting started. Once you’re done with this guide, you will be able to:
  • Get detailed metrics on API usage, errors, and performance
  • Track API adoption and usage by individual consumers
  • Log individual API requests, responses, and correlated application logs
  • See what’s causing slow API requests with traces
  • Monitor uptime and set up custom alerts

Requirements

Requires Node.js 20.6+ or Bun 1.1.13+, with Elysia 1.1+.

Create app

To get started, create a new app in the Apitally dashboard and select Elysia as your framework. Create app You can also configure the environments (e.g. prod and dev) for your app, or simply accept the defaults. After submitting, you will see tailored setup instructions for your app. These include your write token and code snippets you can copy and paste into your project.
The write token (apt_...) provided in the setup instructions uniquely identifies your app for the purpose of data ingestion only. It does not grant any kind of read access to your data.

Install the SDK

Install the Apitally SDK as a dependency in your project.
For Node.js, also install Elysia’s @elysiajs/node adapter.

Initialize Apitally

Register apitallyPlugin with the write token and other configuration options immediately after creating the app, before registering routes or plugins that add routes.
You can also set the APITALLY_WRITE_TOKEN and APITALLY_ENV environment variables instead of passing writeToken and env in code. Deploy your application with these changes, or restart it if you’re testing locally.
The basic setup is now complete. Metrics and logs will start appearing in the Apitally dashboard.

Identify consumers

Consumers are the users or applications calling your API. Identifying them lets you analyze and filter API traffic by consumer in Apitally. Call setConsumer during request handling to associate the current request with a consumer identifier. You can call it wherever the consumer is known, such as in a route handler, a lifecycle hook, or authentication code. You can also provide a display name and group for each consumer.

Capture headers and bodies

Only response headers are captured by default. You can opt in to capture request headers as well as request and response bodies when initializing Apitally.

Mask sensitive information

The SDK automatically masks common sensitive query parameters, headers, and body fields. To mask additional data, pass regular expressions matching query parameter names, header names, or body field names to maskQueryParams, maskHeaders, or maskBodyFields, respectively. Use the i flag for case-insensitive matching.
You can use the maskRequestBody and maskResponseBody callbacks to mask individual fields or entire bodies using custom logic. Use maskLogRecord to mask or drop application log records. More information about masking is available here.

Sample requests

If your application receives a lot of traffic, you may want to sample requests to stay within your request logs quota. Metrics will continue to count every request regardless of sampling. Use sampleRate to capture logs and traces for a fraction of requests.
You can also use the sampleOnRequest and sampleOnResponse callbacks to make sampling decisions using custom logic. More information about sampling is available here.

Instrument third-party libraries

Instrumenting third-party libraries adds details about database queries, HTTP calls to external services, and other operations to your request traces. This lets you see how these operations contribute to API response times. Use OpenTelemetry instrumentation packages to instrument the libraries you want to trace. For example, to trace database queries made with pg, install @opentelemetry/instrumentation and @opentelemetry/instrumentation-pg:
Create an instrumentation.mjs file that registers the instrumentation:
instrumentation.mjs
Load this file before your application code so the instrumentation is registered before pg is imported:
Apitally sets up OpenTelemetry automatically, so you don’t need to create a tracer provider or configure an exporter. More information about tracing and instrumenting third-party libraries is available here.