JavaScript SDK

Installation

npm

npm install @autoblocks/client

yarn

yarn add @autoblocks/client

pnpm

pnpm add @autoblocks/client

Usage in Next.js

If you are using a full-stack web framework like Next.js, you will need to modify your config to set a fallback for fs and child-process. The Autoblocks SDK uses these modules to generate the metadata for replays, but these modules are not available in a browser environment. This ensures that the SDK will work in both the browser and server-side.

next.config.js

const nextConfig = {
  webpack: (config) => {
    config.resolve.fallback = { fs: false, child_process: false };
    return config;
  },
};

module.exports = nextConfig;

AutoblocksTracer

The AutoblocksTracer class is used to send events to Autoblocks. The only required argument is your ingestion key:

JavaScript

import { AutoblocksTracer } from '@autoblocks/client';

const tracer = new AutoblocksTracer(process.env.AUTOBLOCKS_INGESTION_KEY);

The second argument is an optional options object that you can use to further configure the tracer:

  • Name
    traceId
    Type
    string | undefined
    Description

    The traceId for all events sent through the tracer instance.

  • Name
    properties
    Type
    object | undefined
    Description

    The properties that will be sent for all events sent through the tracer instance.

  • Name
    timeout
    Type
    object | undefined
    Description

    The timeout for each sendEvent call, which is a POST request to the Autoblocks ingestion API. Default value is { seconds: 5 }.

JavaScript

const tracer = new AutoblocksTracer(process.env.AUTOBLOCKS_INGESTION_KEY, {
  traceId: 'my-trace-id',
  properties: { source: 'my-app' },
  timeout: { seconds: 1 },
});

sendEvent

Send an event to Autoblocks.

JavaScript

const { traceId } = await tracer.sendEvent('ai.request', {
  properties: {
    provider: 'openai',
    prompt: 'Hello!',
  },
});

Arguments

  • Name
    message
    Type
    string
    Description

    The name of the event.

  • Name
    options
    Type
    object | undefined
    Description

    Optional object containing additional information about the event.

  • Name
    traceId
    Type
    string | undefined
    Description

    Used to group related events together. If not provided, defaults to a random UUID.

  • Name
    properties
    Type
    object | undefined
    Description

    Custom properties to attach to the event. The values in this object can be anything, including other objects.

  • Name
    spanId
    Type
    string | undefined
    Description

    Within a trace, connect related events together using span IDs. This is useful to connect request and response events together, for example.

  • Name
    parentSpanId
    Type
    string | undefined
    Description

    Establish parent / child relationships between your spans using parent span IDs. For example, if some events represent work being done on behalf of a higher level span in your trace, you should set that span's ID as the parent span ID for those events.

  • Name
    timestamp
    Type
    string | undefined
    Description

    An ISO date time string of when the event happened. If not provided, defaults to the current time.

Returns

Returns a Promise containing:

  • Name
    traceId
    Type
    string | undefined
    Description

    The traceId associated with the event. This will be undefined if the event failed to be delivered.

setTraceId

Use this method to set the traceId for all subsequent calls of sendEvent.

JavaScript

tracer.setTraceId('my-trace-id');

Arguments

  • Name
    traceId
    Type
    string
    Description

    The traceId to set.

setProperties

Use this method to set the properties for all subsequent calls of sendEvent.

JavaScript

tracer.setProperties({ x: 1, y: 2 }); // --> { x: 1, y: 2 }
tracer.setProperties({ x: 2 }); // --> { x: 2 }

Arguments

  • Name
    properties
    Type
    object
    Description

    The properties to set.

updateProperties

Use this method to update the properties for all subsequent calls of sendEvent.

JavaScript

tracer.updateProperties({ x: 1, y: 2 }); // --> { x: 1, y: 2 }
tracer.updateProperties({ x: 2 }); // --> { x: 2, y: 2 }

Arguments

  • Name
    properties
    Type
    object
    Description

    The properties to update.

AutoblocksAPIClient

The AutoblocksAPIClient allows you to interact with the Autoblocks REST API. The only required argument is your API key.

JavaScript

import { AutoblocksAPIClient } from '@autoblocks/client';

const client = new AutoblocksAPIClient(process.env.AUTOBLOCKS_API_KEY);

The second argument is an optional options object that you can use to further configure the client:

  • Name
    timeout
    Type
    object | undefined
    Description

    The timeout for each HTTP request to the Autoblocks REST API. Default value is { seconds: 10 }.

JavaScript

const client = new AutoblocksAPIClient(process.env.AUTOBLOCKS_API_KEY, {
  timeout: { seconds: 30 },
});

See the REST API documentation for more details on all of the endpoints exposed through this client.

LangChain Callback

See the section on LangChain in our integration docs.