Skip to content

JavaScript Client (@cognipeer/client-js)

The official JavaScript client library for Cognipeer provides a convenient way to interact with the Cognipeer API from both browser and Node.js environments.

Installation

bash
npm install @cognipeer/client-js

Or with Yarn:

bash
yarn add @cognipeer/client-js

Quick Start

javascript
import CognipeerClient from '@cognipeer/client-js';

// Initialize the client
const client = new CognipeerClient({
  baseUrl: 'https://api.cognipeer.com/v1/client', // Optional, defaults to this value
  token: 'your-api-token' // Required for authentication
});

// List available peers
const peers = await client.peer.list();
console.log(peers);

// Get a specific peer by ID
const peerId = '90aa68af-d5c4-4b60-b63e-b732db08bb46';
const peerDetails = await client.peer.get(peerId);

// Create a conversation with a peer
const conversation = await client.conversation.create(peerId);

// Send a message to a conversation
const conversationId = conversation._id;
const message = await client.conversation.sendMessage(
  conversationId, 
  'Hello, how can you help me?'
);
console.log(message.content);

API Reference

Constructor

javascript
const client = new CognipeerClient(options);

Options

ParameterTypeRequiredDescription
baseUrlstringNoThe base URL for the Cognipeer API. Defaults to 'https://api.cognipeer.com/v1/client'
tokenstringYesAuthentication token for the API

Peer Methods

peer.list(options)

Retrieves a list of all available peers.

javascript
const peers = await client.peer.list({ limit: 20 });
Options
ParameterTypeRequiredDescription
limitnumberNoMaximum number of peers to return (default: 10)
Returns

Promise<Peer[]>

peer.get(id)

Get details of a specific peer by ID.

javascript
const peer = await client.peer.get(peerId);
Parameters
ParameterTypeRequiredDescription
idstringYesThe ID of the peer
Returns

Promise<Peer>

peer.competition(peerId, options)

Send a message to a peer in competition mode (creates a new conversation).

javascript
const response = await client.peer.competition(
  peerId, 
  { 
    content: 'Tell me about your products',
    responseFormat: 'json',
    responseSchema: { type: 'object', properties: {} }, // Optional
    additionalContext: { customData: 'value' } // Optional
  }
);
Parameters
ParameterTypeRequiredDescription
peerIdstringYesThe ID of the peer
optionsobjectYesOptions for the competition
options.contentstringYesThe message content
options.responseFormatstringNoFormat of the response. Can be 'text' (default) or 'json'
options.responseSchemaobjectNoJSON schema for structured responses
options.additionalContextobjectNoAdditional context to provide to the peer
Returns

Promise<Message> or Promise<JsonResponseMessage> depending on the response format

peer.chat(peerId, options)

Chat with a peer by sending a series of messages (creates a new conversation).

javascript
const response = await client.peer.chat(
  peerId,
  {
    messages: [
      { role: 'user', content: 'Hello' },
      { role: 'ai', content: 'Hi there! How can I help you?' },
      { role: 'user', content: 'What services do you offer?' }
    ],
    responseFormat: 'text'
  }
);
Parameters
ParameterTypeRequiredDescription
peerIdstringYesThe ID of the peer
optionsobjectYesOptions for the chat
options.messagesarrayYesArray of message objects, each with role ('user' or 'ai') and content
options.responseFormatstringNoFormat of the response. Can be 'text' (default) or 'json'
options.responseSchemaobjectNoJSON schema for structured responses
options.additionalContextobjectNoAdditional context to provide to the peer
Returns

Promise<Message> or Promise<JsonResponseMessage> depending on the response format

Conversation Methods

conversation.create(peerId)

Creates a new conversation with the specified peer.

javascript
const conversation = await client.conversation.create(peerId);
Parameters
ParameterTypeRequiredDescription
peerIdstringYesThe ID of the peer to create a conversation with
Returns

Promise<Conversation>

conversation.list(filter)

Get a list of conversations with optional filtering.

javascript
const conversations = await client.conversation.list({ peerId: 'some-id' });
Parameters
ParameterTypeRequiredDescription
filterobjectNoFilter criteria for conversations
filter.peerIdstringNoFilter by peer ID
Returns

Promise<Conversation[]>

conversation.get(id)

Get a specific conversation by ID.

javascript
const conversation = await client.conversation.get(conversationId);
Parameters
ParameterTypeRequiredDescription
idstringYesThe ID of the conversation to find
Returns

Promise<Conversation>

conversation.sendMessage(id, content, options)

Sends a message to an existing conversation.

javascript
const message = await client.conversation.sendMessage(
  conversationId, 
  content, 
  { responseFormat: 'json' }
);
Parameters
ParameterTypeRequiredDescription
idstringYesThe ID of the conversation to send a message to
contentstringYesThe content of the message to send
optionsobjectNoAdditional options for the message
options.responseFormatstringNoFormat of the response. Can be 'text' (default) or 'json'
Returns

Promise<Message> or Promise<JsonResponseMessage> depending on the response format

conversation.getMessages(id, messagesCount)

Get messages from a conversation.

javascript
const messages = await client.conversation.getMessages(conversationId, 20);
Parameters
ParameterTypeRequiredDescription
idstringYesThe ID of the conversation to find messages in
messagesCountnumberNoNumber of messages to return (default: 10)
Returns

Promise<Message[]>

Flow Methods

flow.execute(id, options)

Execute a flow with the specified inputs.

javascript
const result = await client.flow.execute(flowId, {
  inputs: {
    name: 'John Doe',
    query: 'How do I reset my password?',
    preferences: { language: 'en' }
    // File inputs are supported and will be handled by the API
  }
});

Note: The legacy app.execute() method is still available for backward compatibility but will be deprecated. Please use flow.execute() for new implementations.

Parameters
ParameterTypeRequiredDescription
idstringYesThe ID of the flow to execute
optionsobjectYesOptions for the flow execution
options.inputsobjectYesObject containing input values keyed by input name
Returns

Promise<Record<string, any>> - The execution results with outputs mapped by name

Error Handling

The client includes built-in error handling that will log API errors to the console. You can also wrap your calls in try/catch blocks for custom error handling:

javascript
try {
  const peers = await client.peer.list();
} catch (error) {
  // Custom error handling
  console.error('Failed to list peers:', error);
}

Running on-premises

For on-premises deployments, just specify your local API endpoint when initializing the client:

javascript
const client = new CognipeerClient({
  baseUrl: 'https://your-local-cognipeer-api.com/v1/client',
  token: 'your-local-token'
});

Built with VitePress