End-to-End Example: AI Model Tracking in Action

This section demonstrates how to configure and use the library to capture both automatic and manual tracking events.

Complete Example

const axios = require('axios');
const { config, interceptAxiosRequest, interceptFetchRequest, captureModel } = require('@handit.ai/node');

// Step 1: Configure the library with API key
config({
  apiKey: 'your-api-key-here',
});

// Step 2: Set up Axios interceptor
const axiosInstance = axios.create();
interceptAxiosRequest(axiosInstance);

// Making a tracked Axios request
axiosInstance.post('/api/model', { input: 'inputData' })
  .then(response => {
    console.log(response.data);  // Automatically tracked
  });

// Step 3: Set up Fetch interceptor
interceptFetchRequest();

// Making a tracked Fetch request
fetch('/api/model', {
  method: 'POST',
  body: JSON.stringify({ input: 'inputData' }),
})
  .then(response => response.json())
  .then(data => {
    console.log(data);  // Automatically tracked
  });

// Step 4: Manually capture model data
captureModel({
  modelId: 'model-456',
  requestBody: { input: 'manual input' },
  responseBody: { output: 'manual output' }
});

Explanation

In this full example, we:

  1. Configured the library with an API key and tracking server URL.

  2. Intercepted and automatically tracked Axios and Fetch requests.

  3. Manually captured model input and output data.

This ensures that all relevant data is tracked, whether it's from automatic interceptors or manual input.

Last updated