API Analytics Documentation

repository·main·Indexed 20 days ago

https://github.com/tom-draper/api-analytics

An API monitoring solution providing a visualization dashboard and a Data API for raw request logs. It offers lightweight middleware for various frameworks, including Node.js (Express, Fastify, Koa, Hono, NestJS, H3, Elysia, Bun, Oak), Python (FastAPI, Flask, Django, Tornado), and Ruby (Rails, Sinatra). Features include configurable privacy levels, custom user ID mapping, and a REST API for retrieving logged request data using an API key.

Tokens
52.3K
Snippets
201
Records
251
Agent score
70%

What's inside API Analytics

  1. Understand collected data and privacy

    main

    Actix Analytics collects pseudo-anonymous data to populate your dashboard. Data is stored in compliance with GDPR.

    Collected Data Points:

    • Path requested
    • Client IP address
    • Client operating system
    • Client browser
    • Request method (GET, POST, etc.)
    • Time of request
    • Status code
    • Response time
    • API hostname
    • API framework (e.g., FastAPI, Flask, Express)

    The API key is the only link between you and the logged data. If lost, you cannot access your analytics.

  2. Configure privacy levels and custom User IDs

    main

    You can control how much client data is stored using privacy_level in the Config object:

    • 0 (default): IP address is used to infer location and is stored.
    • 1: IP address is used to infer location, then discarded.
    • 2: IP address is never accessed and location is never inferred.

    Example setting privacy level 2:

    config = Config()
    config.privacy_level = 2

    You can also define a custom user identifier by providing a get_user_id mapper function. This is useful if you want to track users by an ID found in your own request headers (e.g., an auth token):

    config = Config()
    config.get_user_id = lambda request: request.headers.get('X-AUTH-TOKEN', '')
  3. Understand collected request data

    main

    API Analytics records a specific subset of metadata for every request to populate your dashboard. The collected data includes:

    • Request method (GET, POST, PUT, etc.)
    • Endpoint requested
    • User agent
    • Client IP address (optional, depending on privacy level)
    • Timestamp of the request
    • Response status code
    • Response time
    • Hostname of API
    • API framework in use (FastAPI, Flask, Express, etc.)

    Data is pseudo-anonymous; the API key is the only link between your identity and the logged data.

  4. Core components of @api-analytics/core

    main

    The core logic of the JavaScript middleware is composed of the following exported entities:

    • Config: A configuration class used to customize analytics behavior. It allows you to provide optional mapper functions to override how request data is extracted.
    • Mappers: A collection of default mapper implementations designed for Node.js-style request objects.
    • Analytics: The central client responsible for batching request data and posting it to the analytics server.
    • getIPAddress: A utility function that resolves the client's IP address while respecting the configured privacy settings.
  5. Maintain and update self-hosted API Analytics

    main

    Database Management

    The database is initialized using database/schema.sql. To run custom SQL commands against the analytics database:

    docker exec -it db psql -U postgres -d analytics -c "YOUR SQL COMMAND;"

    Updates

    To update the backend to the latest version (note: this causes downtime):

    docker compose down
    git pull origin main
    docker compose up -d

    Logs

    If you encounter issues, check the following logs:

    • Nginx: docker logs nginx
    • Logger: docker exec -it logger tail requests.log
    • API: docker exec -it api tail api.log
    # Run SQL
    docker exec -it db psql -U postgres -d analytics -c "SELECT 1;"
    
    # Update
    docker compose down
    git pull origin main
    docker compose up -d
    
    # Logs
    docker logs nginx
    docker exec -it logger tail requests.log
    docker exec -it api tail api.log
  6. Install and use Fira Code fonts

    main

    Fira Code is available as a single variable font file or as a collection of static font files.

    Variable Font

    If your application supports variable fonts, use the single file containing the wght axis. This allows you to select intermediate weights between the standard styles.

    • File: FiraCode-VariableFont_wght.ttf

    Static Fonts

    If your application does not support variable fonts, use the individual static files for specific weights:

    • static/FiraCode-Light.ttf
    • static/FiraCode-Regular.ttf
    • static/FiraCode-Medium.ttf
    • static/FiraCode-SemiBold.ttf
    • static/FiraCode-Bold.ttf

    Quickstart

    1. Install the desired font files onto your system.
    2. Use your application's font picker to select the Fira Code family and the desired style.
  7. Add API Analytics middleware to FastAPI

    main

    Install the FastAPI integration using pip:

    pip install api-analytics[fastapi]

    Then, add the Analytics middleware to your FastAPI application instance, providing your api_key.

    import uvicorn
    from fastapi import FastAPI
    from api_analytics.fastapi import Analytics
    
    app = FastAPI()
    app.add_middleware(Analytics, api_key="YOUR-API-KEY")  # Add middleware
    
    @app.get('/')
    async def root():
        return {'message': 'Hello World!'}
    
    if __name__ == "__main__":
        uvicorn.run("app:app", reload=True)
  8. Add API Analytics middleware to Rails

    main

    To monitor a Rails application, add the Analytics::Rails middleware in config/application.rb. You must provide your unique API key generated at apianalytics.dev/generate.

    require 'rails'
    require 'api_analytics'
    
    Bundler.require(*Rails.groups)
    
    module RailsMiddleware
      class Application < Rails::Application
        config.load_defaults 6.1
        config.api_only = true
    
        config.middleware.use ::Analytics::Rails, "YOUR-API-KEY"  # Add middleware
      end
    end