VonageClient is the main entry point for the Vonage Client SDK.

Hierarchy

  • CombinedClientJS
    • VonageClient

Methods

  • Clear all callbacks for an event.

    Type Parameters

    • T extends "mute" | "conversationEvent" | "callHangup" | "callMediaDisconnect" | "legStatusUpdate" | "rtcStatsUpdate" | "callInvite" | "callInviteCancel" | "sessionError" | "callMediaError" | "callTransfer" | "earmuff" | "dtmf" | "callMediaReconnecting" | "callMediaReconnection" | "reconnecting" | "reconnection"

    Parameters

    • event: T

      the event to unregister from (e.g. 'legStatusUpdate')

    Returns void

    void

    Example

    client.clearCallbacks('sessionError');
    

    Remarks

    This is useful for cleaning up callbacks when you no longer need them.

  • Create a session with a token and optional sessionId If no sessionId is provided, a new one will be generated and returned. If a sessionId is provided, it will be used to resume an existing session.

    Parameters

    • token: string
    • Optional sessionId: null | string

      optional sessionId to use

    Returns Promise<string>

    the sessionId of the session

    Example

    const token = 'token';

    try {
    const sessionId: string = await client.createSession(token);
    console.log({ sessionId });
    } catch (e) {
    console.log({ e });
    }
  • Get a Conversation

    Parameters

    • conversationIdOrName: string

      the Conversation's id or conversation name

    Returns Promise<Conversation>

    the Conversation

    Example

    try {
    const conversation: Conversation = await client.getConversation(
    'conversationIdOrName'
    );
    console.log({ conversation });
    } catch (e) {
    console.log({ e });
    }
  • Unregister a callback for an event.

    Type Parameters

    • T extends "mute" | "conversationEvent" | "callHangup" | "callMediaDisconnect" | "legStatusUpdate" | "rtcStatsUpdate" | "callInvite" | "callInviteCancel" | "sessionError" | "callMediaError" | "callTransfer" | "earmuff" | "dtmf" | "callMediaReconnecting" | "callMediaReconnection" | "reconnecting" | "reconnection"

    Parameters

    • event: T

      the event to register for (e.g. 'legStatusUpdate')

    • callbackSymbol: symbol

      the callback symbol to unregister

    Returns boolean

    true if the callback was unregistered, false otherwise

    Example

    client.off('reconnecting', reeconnectingListener);
    client.off('reconnection', reeconnectionListener);
    client.off('sessionError', sessionErrorListener);

    Remarks

    We recommend deregistering callbacks when you no longer need them.

  • Register a callback for an event.

    Type Parameters

    • T extends "mute" | "conversationEvent" | "callHangup" | "callMediaDisconnect" | "legStatusUpdate" | "rtcStatsUpdate" | "callInvite" | "callInviteCancel" | "sessionError" | "callMediaError" | "callTransfer" | "earmuff" | "dtmf" | "callMediaReconnecting" | "callMediaReconnection" | "reconnecting" | "reconnection"

    • P extends ((event) => void) | ((callId, callQuality, reason) => void) | ((callId, reason) => void) | ((callId, legId, legStatus) => void) | ((stats, callId) => void) | ((callId, reason) => void) | ((callId, from, channelType) => void) | ((reason) => void) | ((callId, error) => void) | ((callId, legId, isMuted) => void) | ((callId, conversationId) => void) | ((callId, legId, earmuffStatus) => void) | ((callId, legId, digits) => void) | ((callId) => void) | ((callId) => void) | (() => void) | (() => void)

    Parameters

    • event: T

      the event to register for (e.g. 'legStatusUpdate')

    • callback: P

      the callback to register for the event

    Returns symbol

    a symbol that can be used to unregister the callback

    Example

    const eventHandler = (event: ConversationEvent) => {
    if (event.kind == 'member:invited') {
    console.log(`Member invited: ${event.body.memberId}`);
    } else if (event.kind == 'member:joined') {
    console.log(`Member joined: ${event.body.memberId}`);
    } else if (event.kind == 'member:left') {
    console.log(`Member left: ${event.body.memberId}`);
    } else if (event.kind == 'ephemeral') {
    console.log(`Ephemeral event: ${event.body}`);
    } else if (event.kind == 'custom') {
    console.log(`Custom event: ${event.body}`);
    } else if (event.kind == 'message:text') {
    console.log(`Text message: ${event.body.text}`);
    } else if (event.kind == 'message:custom') {
    console.log(`Custom message: ${event.body.customData}`);
    } else if (event.kind == 'message:image') {
    console.log(`Image message: ${event.body.imageUrl}`);
    } else if (event.kind == 'message:video') {
    console.log(`Video message: ${event.body.videoUrl}`);
    } else if (event.kind == 'message:audio') {
    console.log(`Audio message: ${event.body.audioUrl}`);
    } else if (event.kind == 'message:file') {
    console.log(`File message: ${event.body.fileUrl}`);
    } else if (event.kind == 'message:vcard') {
    console.log(`Vcard message: ${event.body.vcardUrl}`);
    } else if (event.kind == 'message:location') {
    console.log(`Location message: ${event.body.location}`);
    } else if (event.kind == 'message:template') {
    console.log(`Template message: ${event.body.template}`);
    } else if (event.kind == 'event:delete') {
    console.log(`Template message: ${event.body}`);
    } else if (event.kind == 'message:seen') {
    console.log(`Template message: ${event.body}`);
    } else if (event.kind == 'message:delivered') {
    console.log(`Message delivered event: ${event.body}`);
    } else if (event.kind == 'message:rejected') {
    console.log(`Message rejected event ${event.body}`);
    } else if (event.kind == 'message:submitted') {
    console.log(`Message delivered event ${event.body}`);
    } else if (event.kind == 'message:undeliverable') {
    console.log(`Message undeliverable event ${event.body}`);
    } else {
    exhaustiveCheck(event);
    }
    };

    const listener = client.on('conversationEvent', eventHandler);

    client.off('conversationEvent', listener);

    Remarks

    Be sure to store the symbol returned by this method so you can unregister the callback later. We recommend unregistering callbacks when you no longer need them. See off.

  • Set a configuration for the client SDK

    Parameters

    Returns void

    void

    Example

    const config: ClientConfig = new ClientConfig(ConfigRegion.EU);
    config.apiUrl = 'https://api.example.com';
    config.websocketUrl = 'https://ws.example.com';
    config.rtcStatsTelemetry = true;
    config.autoReconnectMedia = false;
    client.setConfig(config);
    // or
    client.setConfig({
    apiUrl: 'https://api.example.com',
    websocketUrl: 'https://ws.example.com'
    });
    client.setConfig({
    region: 'EU'
    });
  • Updates a conversation object identified by its unique conversation ID.

    This method overrides the conversation properties to the provided parameters and rest remains as it is.

    Parameters

    • conversationId: string

      the Conversation's id.

    • parameters: UpdateConversationParameters

      The properties of the conversation. These will replace existing values to the provided ones, rest will remain as they are.

    Returns Promise<Conversation>

    conversation, this object will contain the updated conversation properties if the update is successful.

    Example

    const conversation = await client.updateConversation('ConversationID', {
    // Update Name
    name: some('name'),
    // Reset Display Name
    displayName: some(null),
    // Update imageUrl
    imageUrl: 'imageUrl',
    // Leave TTL as is (implicitly via omission)
    // Leave customSortKey as is (explicitly via none)
    customSortKey: none(),
    customData: { key: 'value' }
    });

Chat

  • Create a conversation

    Parameters

    Returns Promise<string>

    the cid of the conversation

    Example

    const conversationId = await client.createConversation({
    name: 'name',
    displayName: 'displayName',
    imageUrl: 'imageUrl',
    ttl: 60,
    customSortKey: 'customSortKey',
    customData: { key: 'value' }
    });
  • Delete a Conversation

    Parameters

    • id: string

      the Conversation's id

    Returns Promise<void>

    void

    Example

    await client.deleteConversation('CONVERSATION_ID');
    
  • Delete an Event in a Conversation

    Parameters

    • id: number

      the id for the Event to be deleted

    • conversationId: string

      the id for the conversation, the event belongs to.

    Returns Promise<void>

    void

    Example

    await client.deleteEvent(123, 'CONVERSATION_ID');
    
  • Get a Conversation's Events

    Parameters

    Returns Promise<EventsPage>

    a EventsPage containing the events

    Example

    const { events, nextCursor, previousCursor } =
    await client.getConversationEvents('CONVERSATION_ID', {
    order: PresentingOrder.DESC,
    pageSize: 10,
    cursor: undefined,
    eventFilter: ['message', 'member:joined'], // event filter query
    includeDeletedEvents: false,
    startId: 3 // start event id
    });
  • Get a Member of a Conversation

    Parameters

    • cid: string

      the Conversation's id

    • mid: string

      the Member's id

    Returns Promise<Member>

    the Member

    Example

    const { id, state, channel } = await client.getConversationMember(
    'CONVERSATION_ID',
    'MEMBER_ID'
    );
    console.log(
    `Member ID: ${id}, State: ${state}, Channel Type: ${channel?.type}`
    );
  • Get a Conversation's Members

    Parameters

    Returns Promise<MembersPage>

    a MembersPage containing the members

    Example

    const { members, nextCursor, previousCursor } =
    await client.getConversationMembers('CONVERSATION_ID', {
    order: PresentingOrder.ASC,
    pageSize: 10
    });
  • Get a list of Conversations for the user.

    Parameters

    Returns Promise<ConversationsPage>

    a ConversationsPage containing the conversations

    Example

    const { conversations, nextCursor, previousCursor } =
    await client.getConversations({
    order: PresentingOrder.DESC,
    pageSize: 10,
    cursor: undefined,
    includeCustomData: true,
    orderBy: OrderBy.CUSTOM_SORT_KEY
    });
  • Invite a user to a Conversation by user's name

    Parameters

    • id: string

      the Conversation's id

    • name: string

      the name of the user to invite

    Returns Promise<string>

    the memberId of the member

    Example

    const memberId = await client.inviteToConversation(
    'CONVERSATION_ID',
    'USERNAME'
    );
  • Join a Conversation

    Parameters

    • id: string

      the Conversation's id

    Returns Promise<string>

    the memberId of the member

    Example

    const memberId = await client.joinConversation('CONVERSATION_ID');
    
  • Leave a Conversation

    Parameters

    • id: string

      the Conversation's id

    Returns Promise<void>

    void

    Example

    await client.leaveConversation('CONVERSATION_ID');
    
  • Send a Custom event to a Conversation

    Parameters

    • id: string

      the Conversation's id

    • eventType: string
    • customData: CustomData

      the body of the event

    Returns Promise<string>

    the timestamp of the message

    Example

    const customData = {
    key1: 'val1',
    key2: 'val2'
    };

    const timestamp = await client.sendCustomEvent(
    'CONVERSATION_ID',
    'custom:test',
    customData // or JSON.stringify(customData)
    );
  • Send an ephemeral event to a Conversation

    Parameters

    • id: string

      the Conversation's id

    • customData: CustomData

      the body of the event

    Returns Promise<string>

    the timestamp of the message

    Example

    const memberId = 'MEMBER_ID';
    const readMessageEvent = {
    type: `${memberId}:readUpdate`,
    eventId: 'EVENT_ID',
    timestamp: 'TIMESTAMP'
    };

    const timestamp = await client.sendEphemeralEvent(
    'CONVERSATION_ID',
    readMessageEvent // or JSON.stringify(readMessageEvent)
    );
  • Send a audio message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • audioUrl: URL

      the url of the audio resource.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageAudioEvent(
    'CONVERSATION_ID',
    audioURL
    );
  • Send a custom message to a Conversation

    Parameters

    • id: string

      the Conversation's id

    • customData: CustomData

      the body of the message

    Returns Promise<string>

    the timestamp of the message

    Example

    const attachmentPayload = {
    attachment: {
    payload: {
    buttons: [
    {
    payload: '{"<cid>": "$","action": "connect","mid": "<mid>"}',
    title: 'Connect to an agent',
    type: 'postback'
    }
    ],
    template_type: 'button',
    text: "Hi, My name is Chatbot, Welcome to my chatbot's corner. How can We help you"
    },
    type: 'template'
    }
    };

    const timestamp = await client.sendMessageCustomEvent(
    'CONVERSATION_ID',
    attachmentPayload // or JSON.stringify(attachmentPayload)
    );
  • Send a file message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • fileUrl: URL

      the url of the file resource.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageFileEvent(
    'CONVERSATION_ID',
    fileURL
    );
  • Send a Image message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • imageUrl: URL

      the url of the image resource.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageImageEvent(
    'CONVERSATION_ID',
    imageUrl
    );
  • Send a Location message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • location: Location

      the description of the location.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageLocationEvent('CONVERSATION_ID', {
    latitude: 'LATITUDE',
    longitude: 'LONGITUDE',
    name: 'Name', //optional
    address: 'Address' //optional
    });
  • Send a message seen event to a Conversation

    Parameters

    • id: number

      the event id

    • conversationId: string

      the conversation id

    Returns Promise<string>

    the timestamp of the message

    Example

    const eventId = 0;

    const timestamp = await client.sendMessageSeenEvent(
    eventId,
    'conversation_id'
    );
  • Send a template message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • templateObject: Template

      the description(name and paramters) of the template.

    • whatsappObject: Whatsapp

      the description(locale and policies of business account) of the location.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageTemplateEvent(
    'CONVERSATION_ID',
    {
    name: 'name',
    parameters: ['Params1'] // optional
    },
    {
    policy: '',
    locale: ''
    }
    );
  • Send a text message to a Conversation

    Parameters

    • id: string

      the Conversation's id

    • text: string

      the Body of the message

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageTextEvent(
    'CONVERSATION_ID',
    'Hello World!'
    );
  • Send a vcard message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • vCardUrl: URL

      the url of the vCardUrl resource.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageVCardEvent(
    'CONVERSATION_ID',
    vCardUrl
    );
  • Send a Vidoe message to a Conversation.

    Parameters

    • id: string

      the Conversation's id

    • videoUrl: URL

      the url of the video resource.

    Returns Promise<string>

    the timestamp of the message

    Example

    const timestamp = await client.sendMessageVideoEvent(
    'CONVERSATION_ID',
    videoURL
    );

Voice

  • Answer a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.answer(callId);
  • Unearmuff your leg of a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.disableEarmuff(callId);
  • Earmuff your leg of a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.enableEarmuff(callId);
  • Experimental

    Get the HTML Audio Element for the SDK. It can be used to route output to other devices.

    Returns undefined | HTMLAudioElement

  • Get a Call's Legs

    Parameters

    Returns Promise<LegsPage>

    a LegsPage containing the legs

    Example

    const { legs, nextCursor, previousCursor } = await client.getCallLegs(
    'callId',
    {
    order: PresentingOrder.DESC,
    pageSize: 10,
    cursor: undefined
    }
    );
  • Get the Leg for a call

    Parameters

    • legId: string

      The Leg Id

    Returns Promise<LegJS>

  • Experimental

    Get the Peer Connection for a call

    Parameters

    • id: string

      The Call Id

    Returns RTCPeerConnection

  • Hangup a call.

    Parameters

    • callId: string

      the callId of the call to hangup

    • Optional reasonText: string

      optional reason text to send to the other party

    • Optional reasonCode: string

      optional reason code to send to the other party

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.hangup(callId);
  • Mute your leg of a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.mute(callId);
  • Reconnect a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.reconnectCall(callId);
  • Reject a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.reject(callId);
  • Sends a TTS message to the Call

    Parameters

    • callId: string

      the callId of the call to send the message to

    • text: string

      the text to send

    Returns Promise<void>

    void

  • Sends a TTS message to the Call

    Parameters

    • callId: string

      the callId of the call to send the message to

    • params: CallSayParams

      the CallSayParams to send

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.say(callId, 'Hello World');
    const ssmlExample =
    '<speak>Hello World <break strength="weak"/> I am using <say-as interpret-as="character">SSML</say-as> <break strength="weak" /> how cool is that? </speak>';
    await client.say(callId, {
    text: ssmlExample,
    voiceName: 'Kimberly',
    ssml: true,
    level: 1,
    loop: 2
    });
  • Send a string of digits to a call via DTMF

    Parameters

    • callId: string

      Call ID

    • digits: string

      DTMF digits

    Returns Promise<void>

    void

    Example

    const callId = 'callId';
    await client.sendDTMF(callId, '1234');
  • Make a server call to the Vonage API. This is used to initiate a call using the Voice API and NCCO.

    Parameters

    • Optional context: Json

      the context to send to the server passed as Custom data to the voice answer webhook

    Returns Promise<string>

    the callId of the call

    Example

    const context = {
    callee: 'user1'
    };

    const callId = await client.serverCall(context);
  • Unmute your leg of a call

    Parameters

    • callId: string

      Call ID

    Returns Promise<void>

    void