|
| 1 | +--- |
| 2 | +title: Apitally - ElysiaJS |
| 3 | +head: |
| 4 | + - - meta |
| 5 | + - property: 'og:title' |
| 6 | + content: Apitally - ElysiaJS |
| 7 | + |
| 8 | + - - meta |
| 9 | + - name: 'description' |
| 10 | + content: We may use Apitally to capture API metrics and logs. |
| 11 | + |
| 12 | + - - meta |
| 13 | + - name: 'og:description' |
| 14 | + content: We may use Apitally to capture API metrics and logs. |
| 15 | +--- |
| 16 | + |
| 17 | +# Apitally |
| 18 | + |
| 19 | +[Apitally](https://apitally.io/elysia) is a simple API monitoring and analytics tool with an official plugin for Elysia. |
| 20 | + |
| 21 | +It provides real-time insights into API usage, errors, and performance. It also captures API request logs and application logs, |
| 22 | +which are automatically correlated. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Basic setup |
| 27 | + |
| 28 | +1. [Sign up](https://app.apitally.io/?signup) for an account, create a new app, and grab your client ID. |
| 29 | + |
| 30 | +2. Install the open-source [Apitally SDK](https://github.com/apitally/apitally-js): |
| 31 | + |
| 32 | +```bash |
| 33 | +bun add apitally |
| 34 | +``` |
| 35 | + |
| 36 | +3. Add the plugin to your Elysia instance and pass in your client ID: |
| 37 | + |
| 38 | +```typescript |
| 39 | +import { Elysia } from "elysia"; |
| 40 | +import { apitallyPlugin } from "apitally/elysia"; |
| 41 | + |
| 42 | +const app = new Elysia() |
| 43 | + .use( |
| 44 | + apitallyPlugin({ |
| 45 | + clientId: "your-client-id", |
| 46 | + env: "dev", // or "prod" etc. |
| 47 | + }), |
| 48 | + ) |
| 49 | + .get("/", () => "hello"); |
| 50 | +``` |
| 51 | + |
| 52 | +A more detailed [setup guide for Elysia](https://docs.apitally.io/frameworks/elysia) is available in the Apitally docs. |
| 53 | + |
| 54 | +## Consumers |
| 55 | + |
| 56 | +You can associate requests with consumer identifiers, allowing you get insights into API adoption and filter logs and metrics by consumer. |
| 57 | + |
| 58 | +```typescript |
| 59 | +app.derive(async ({ apitally, jwt, cookie: { auth } }) => { |
| 60 | + const profile = await jwt.verify(auth); |
| 61 | + apitally.consumer = { |
| 62 | + identifier: profile.id, |
| 63 | + name: profile.name, // optional |
| 64 | + group: profile.role, // optional |
| 65 | + }; |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +## Logs |
| 70 | + |
| 71 | +Capturing request and applications logs is disabled by default. You can enable it by passing the `requestLogging` option to the plugin and |
| 72 | +configure in detail what's included in the logs. |
| 73 | + |
| 74 | +```typescript |
| 75 | +import { Elysia } from "elysia"; |
| 76 | +import { apitallyPlugin } from "apitally/elysia"; |
| 77 | + |
| 78 | +const app = new Elysia() |
| 79 | + .use( |
| 80 | + apitallyPlugin({ |
| 81 | + clientId: "your-client-id", |
| 82 | + env: "dev", // or "prod" etc. |
| 83 | + requestLogging: { // [!code ++] |
| 84 | + enabled: true, // [!code ++] |
| 85 | + logRequestHeaders: true, // [!code ++] |
| 86 | + logRequestBody: true, // [!code ++] |
| 87 | + logResponseBody: true, // [!code ++] |
| 88 | + captureLogs: true, // application logs // [!code ++] |
| 89 | + }, // [!code ++] |
| 90 | + }), |
| 91 | + ) |
| 92 | + .get("/", () => "hello"); |
| 93 | +``` |
0 commit comments