Tiledesk Docs
Search…
NodeJS SDK
Full NodeJS API reference is hosted on Github

Introduction

Follow this guide to use the Tiledesk JavaScript SDK in your Node.js application.
Before you can add Tiledesk 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 project APIs, a project secret for custom authentication and all the stuff need to work with Tiledesk and his APIs.

Add Tiledesk to your project

Install TiledeskClient library with npm command:
1
npm install @tiledesk/tiledesk-client
Copied!
Alternatively use package.json to import the library in the "dependencies" property, as in the following example:
1
{
2
"name": "Hello Tiledesk nodeJS",
3
"version": "1.0.0",
4
"description": "",
5
"main": "index.js",
6
"scripts": {
7
"test": "echo \"Error: no test specified\" && exit 1",
8
"start": "node index.js"
9
},
10
"keywords": [],
11
"author": "",
12
"license": "ISC",
13
"dependencies": {
14
"@tiledesk/tiledesk-client": "^0.8.4"
15
}
16
}
Copied!
Then run
1
npm install
Copied!
Once installed you can import TiledeskClient() class in your Node.js file using the "require" command:
1
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
Copied!

Authentication

Before you can interact with Tiledesk APIs you need to authenticate. Tiledesk provides three authentication methods.
  1. 1.
    Authentication with email and password
  2. 2.
    Authentication as anonymous user
  3. 3.
    Custom authentication

Authentication with email and password

This is the authentication method that you need when working with Tiledesk APIs. Every API methods, 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.
1
TiledeskClient.authEmailPassword(
2
'APIKEY',
3
/* EMAIL */,
4
/* PASSWORD */,
5
null,
6
function(err, result) {
7
if (!err && result) {
8
console.log('You got your auth token!', result.token);
9
console.log('Your user ID!', result.user._id);
10
}
11
else {
12
console.err("An error occurred", err);
13
}
14
});
Copied!
In response you will get a token to interact with APIs using your account and the corresponding user ID.

Authentication as Anonymous user

This authentication method is useful for anonymous user that need to interact with support APIs
1
TiledeskClient.anonymousAuthentication(
2
PROJECT_ID,
3
APIKEY,
4
null,
5
function(err, result) {
6
assert(result.token != null);
7
let token = result.token;
8
}
9
);
Copied!
In response you will get a token to interact with APIs in anonymous mode.

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:
1
npm install uuid
Copied!
1
const { v4: uuidv4 } = require('uuid');
2
var externalUserId = uuidv4();
3
var externalUser = {
4
_id: externalUserId,
5
firstname:"John",
6
lastname:"Wick",
8
};
9
var signOptions = {
10
subject: 'userexternal',
11
audience: 'https://tiledesk.com/projects/' + YOUR_PROJECT_ID
12
};
13
var jwtCustomToken = "JWT " + jwt.sign(externalUser, YUOR_PROJECT_SECRET, signOptions);
14
15
TiledeskClient.customAuthentication(
16
jwtCustomToken,
17
APIKEY,
18
null,
19
function(err, result) {
20
if (!err && result) {
21
let token = result.token;
22
}
23
}
24
);
Copied!

The TiledeskClient class

To interact with Tiledesk APIs you need to create an instance of a TiledeskClient() class using his constructor. You MUST supply an APIKEY, an existing Project ID and a valid token, this last one got through some of the authentication methods above.
In the next example we first authenticate using our user credentials, then we create a new TiledeskClient instance using a PROJECT_ID and the token we got from authentication:
1
TiledeskClient.authEmailPassword(
2
'APIKEY',
3
/* EMAIL */,
4
/* PASSWORD */,
5
null,
6
function(err, result) {
7
if (!err && result) {
8
console.log('You got the token!', result.token);
9
const tdclient = new TiledeskClient({
10
APIKEY: /* APIKEY */,
11
projectId: /* PROJECT_ID */,
12
token: result.token
13
});
14
}
15
else {
16
console.err("An error occurred", err);
17
}
18
});
Copied!

Working with support requests

A Support Request is a set of metadata and messages that describe whole conversation. A Support Request contains data regarding the Request status (open/assigned/closed etc.), the web/app source page of the conversation, the end-user ID, his email etc. The main information consist of the messages sent and received by the request. Using messaging APIs is indeed the most common way to interact with the request.
Yiou 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

To create a support request you simply send a message to a not-existing request ID. A new Request object is automcatically created by Tiledesk every time you send a message to a no-existing request ID.
It's up to you to create a new, UNIQUE request ID, following the Tiledesk rules. If you don't want to know how to create a new request ID, to get a new one you can simply use the function TiledeskClient.newRequestId() passing PROJECT_ID as a parameter. Sending a message to this newly created Request ID will automatically create a new Request Object, as in the following example:
1
const text_value = 'test message';
2
const request_id = TiledeskClient.newRequestId(PROJECT_ID);
3
tdclient.sendSupportMessage(
4
request_id,
5
{text: text_value},
6
(err, result) => {
7
assert(err === null);
8
assert(result != null);
9
assert(result.text === text_value);
10
});
Copied!
As soon as you send a new message to Tiledesk with the new requestID, the request is created and ready to be processed.
With the same sendSupportMessage() function you can send additional messages to the request. In this example we send a second message to the request using the same request id we used to create the request in the previous example.
1
tdclient.sendSupportMessage(
2
request_id,
3
{text: 'second message'},
4
(err, result) => {
5
assert(err === null);
6
assert(result != null);
7
assert(result.text === text_value);
8
});
Copied!
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

1
let REQUEST_ID = /* THE REQUEST ID */;
2
tdclient.getRequestById(REQUEST_ID, (err, result) => {
3
const request = result;
4
if (request.request_id != null) {
5
console.log("Got request with first text:", request.first_text);
6
}
7
});
Copied!

Query support requests

1
tdclient.getAllRequests(
2
{
3
limit: 1,
4
status: TiledeskClient.UNASSIGNED_STATUS
5
},
6
(err, result) => {
7
assert(result);
8
const requests = result.requests;
9
assert(requests);
10
assert(result.requests);
11
assert(Array.isArray(requests));
12
assert(result.requests.length > 0);
13
}
14
);
Copied!

Working with teamates

A Project's teammate is a user who collaborates with you on a specific project.
While the name on the User Interface and documentaion level is always teammate, on the APIs level a teamate is called ProjectUser. As the the name suggests, a ProjectUser is a Tiledesk User invited with a specific role on a specific Project.

Update teamate status to available/unavailable

With TiledeskClient.updateProjectUserCurrentlyLoggedIn() you will update the status of the user token in the TiledeskClient constructor.
1
const tdclient = new TiledeskClient({
2
APIKEY: /* APIKEY */,
3
projectId: /* PROJECT_ID */,
4
token: result.token
5
});
6
tdclient.updateProjectUserCurrentlyLoggedIn(
7
{
8
user_available: true
9
},
10
function(err, result) {
11
if (!err && result) {
12
assert(result);
13
assert(result.user_available === true);
14
}
15
}
16
);
Copied!

Check teamate status

1
const tdclient = new TiledeskClient({
2
APIKEY: /* APIKEY */,
3
projectId: /* PROJECT_ID */,
4
token: result.token
5
});
6
tdclient.getProjectUser(
7
USER_ID,
8
function(err, result) {
9
if (!err && result) {
10
assert(Array.isArray(result));
11
assert(result[0]._id != null);
12
assert(result[0].user_available === true);
13
let PROJECT_USER_ID = result[0]._id;
14
}
15
else {
16
assert.ok(false);
17
}
18
}
19
);
Copied!
The PROJECT_USER_ID variable is the teamate ID of your user (USER_ID) on the PROJECT_ID you specified in the TiledeskClient custructor.

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:
1
TiledeskClient.authEmailPassword(
2
APIKEY,
3
EMAIL,
4
PASSWORD,
5
{
6
APIURL: API_ENDPOINT
7
}
8
});
Copied!

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 config.APIRUL:
1
const tdclient = new TiledeskClient({
2
APIKEY: APIKEY,
3
projectId: PROJECT_ID,
4
token: YOUR_TOKEN,
5
APIURL: API_ENDPOINT
6
})
Copied!
Last modified 15d ago