NodeJS SDK
Full NodeJS API reference is hosted on Github
Introduction
With this guide you will learn how to use the Tiledesk JavaScript SDK in your Node.js application. Tiledesk Javascript SDK is built on top of Tiledesk REST APIs.
Before you add Tiledesk SDK to your Node.js app, you need a Tiledesk account and a Tiledesk project. Once you create your project you will have a projectID, a user to play with the project's APIs, a project secret for custom authentication and all the stuff needed to work with Tiledesk and his APIs.
Add Tiledesk to your project
Install TiledeskClient library with npm:
npm install @tiledesk/tiledesk-clientAlternatively use package.json to import the library in the "dependencies" property, as in the following example:
{
"name": "Hello Tiledesk nodeJS",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@tiledesk/tiledesk-client": "^0.8.28"
}
}Then run:
Once installed you can import TiledeskClient() class in your Node.js file using the "require" command:
All interaction with Tiledesk APIs uses the TiledeskClient class.
Create a TiledeskClient instance
Before you can use most of the APIs you need to create a TiledeskClient instance. To create the client instance you need a Tiledesk API Key, the project id and an authentication token:
Additional initialization options:
{string} options.APIKEY Mandatory. Tiledesk APIKEY
{string} options.projectId Mandatory. Tiledesk projectId. Will be used in each call on project's APIs.
{string} options.token Mandatory. Tiledesk authentication token. Will be used in each call on project's APIs.
{string} options.APIURL Optional. Tiledesk server API endpoint.
{boolean} options.log Optional. If true HTTP requests are logged.
The most important option is APIURL that provides the opportunity to use TiledeskClient with your own installation of the Tiledesk server.
You can get the token using some authentication method. The token is also provided automatically while you interact with the chatbot using APIs.
Authentication
Before you can interact with Tiledesk APIs you need to authenticate. Tiledesk provides three static authentication methods. Use the stepper below for each.
Authentication with email and password
This is the authentication method that you need when working with Tiledesk APIs. Every API method, except authentication ones, work on a project + role + token basis. To authenticate and get a token with email and password use the authEmailPassword() method from the TiledeskClient class. You must provide the APIKEY to authenticate. Actually APIKEYs are experimental and can be omitted. Just use the string 'APIKEY' in place of the real one.
In response you will get a token to interact with APIs using your account and the corresponding user ID.
Custom authentication
With custom authentication you can work with your own users making them auto-signin in Tiledesk without previous signup. This can be used in the place of anonymous authentication to certify users coming from external application, giving them a certified identity in Tiledesk.
For this example import uuid:
Example:
The TiledeskClient class
To interact with Tiledesk APIs you need to create an instance of a TiledeskClient class using its constructor. You MUST supply an APIKEY, an existing Project ID and a valid token (obtained through one of the authentication methods above).
Example: authenticate and create a client instance using the returned token:
Working with support requests
A Support Request is a set of metadata and messages decorating a conversation. A Support Request contains data regarding the status (open/assigned/closed etc.), the web/app source page of the conversation, the end-user-id, his email etc. The main information consists of the messages sent and received by the request. Using messaging APIs is indeed the most common way to interact with the request.
You can interact with the request messages using Messaging APIs. Or you can interact directly with Request's metadata using the Request APIs.
Create a support request (send a message)
To create a support request you simply send a message to a not-existing request-id. A new Request object is automatically created by Tiledesk every time you send a message to a non-existing request-id.
You can create a new, UNIQUE request-id using TiledeskClient.newRequestId(PROJECT_ID). Sending a message to this newly created Request ID will automatically create a new Request object, as in the following example:
As soon as you send a new message to Tiledesk with the new request-id, the request is created and ready.
Send messages to a Support Request
With the same sendSupportMessage() function you can send additional messages to the request's conversation. Example sending a second message to the same request:
With Tiledesk you can also get sent messages to a request's conversation using Webhooks, subscribing to the Message.create event.
Get a support request by id
Query support requests
Working with teammates
A Project's teammate is a user who collaborates with you on a specific project.
While the name on the User Interface and documentation level is always teammate, on the APIs level a teammate is called ProjectUser. As the name suggests, a ProjectUser is a Tiledesk User invited with a specific role on a specific Project.
Update teammate status to available/unavailable
With TiledeskClient.updateProjectUserCurrentlyLoggedIn() you will update the status of the user token in the TiledeskClient constructor.
Check teammate status
The PROJECT_USER_ID variable is the teammate ID of your user (USER_ID) on the PROJECT_ID you specified in the TiledeskClient constructor.
Switch between Cloud and Self hosted instances
Self hosted option
These APIs automatically work with the Tiledesk cloud instance.
If you are running your own self-hosted instance of Tiledesk, the APIs provide a specific option to select your endpoint.
Specify the API endpoint in class methods
If you are using a class method, i.e. authentication methods, use the options.APIURL parameter to specify the endpoint, as in the following example:
Specify the API endpoint in instance methods
If instead you are using instance methods working with an instance of TiledeskClient, you must specify the parameter in the constructor config object as APIURL:
Links
Last updated 2 years ago
This site uses cookies to deliver its service and to analyze traffic. By browsing this site, you accept the privacy policy.