Javascript SDK

Our Javascript SDK makes it straightforward to integrate with Context.ai.

Installation

Install the Context Javascript SDK with NPM:

npm install @contextco/context-node

Usage

The Javascript SDK needs to be authenticated with your Context.ai API Key. You can generate this key from the onboarding page (if you have not yet sent any transcripts to Context.ai), or from the settings page if you are already integrated.

Once you have your API key, you can start sending requests immediately using one of the example code snippets below. The SDK is usable in Javascript and Typescript applications.

Typescript Example

import { ContextAPI, ContextAPIOptionalParams, KnownMessageRole, Credential } from "@contextco/context-node";

const options: ContextAPIOptionalParams = {
  credential: new Credential(process.env.CONTEXT_TOKEN),
};
const c = new ContextAPI(options);

await c.log.conversation({
  body: {
    conversation: {
      messages: [
        { message: "You are a helpful assistant.", role: KnownMessageRole.System, rating: 0 },
        { message: "Hi, how are you doing?", role: KnownMessageRole.User, rating: 0 },
        { message: "I'm doing super well, how are you?", role: KnownMessageRole.Assistant, rating: 1 }
      ],
    }
  }
})

await c.log.conversationThread({
  body: {
    conversation: {
      id: "c_vay3wb34br39gkoq",
      messages: [
        { message: "You are a very helpful assistant.", role: KnownMessageRole.System, rating: 0 },
        { message: "Hi there, how are you doing?", role: KnownMessageRole.User, rating: 0 },
        { message: "I'm doing super well, how are you?", role: KnownMessageRole.Assistant, rating: 1 }
      ],
    }
  }
});

Javascript Example

import { ContextAPI, KnownMessageRole, Credential } from "@contextco/context-node";

const options = {
  credential: new Credential(process.env.CONTEXT_TOKEN),
};
const c = new ContextAPI(options);

await c.log.conversation({
  body: {
    conversation: {
      messages: [
        { message: "You are a helpful assistant.", role: KnownMessageRole.System, rating: 0 },
        { message: "Hi, how are you doing?", role: KnownMessageRole.User, rating: 0 },
        { message: "I'm doing super well, how are you?", role: KnownMessageRole.Assistant, rating: 1 }
      ],
    }
  }
})

Github: https://github.com/contextco/context-node/tree/main

Last updated