Axios is used to make HTTP requests. A default Axios client will be used by the library, but a custom instance may be provided.
constDeskproClient=require('@deskpro/deskpro-api-client-javascript');consthttpClient=axios.create({timeout:1000});constclient=newDeskproClient('http://deskpro.company.com',httpClient);// Or use the setter method.// client.setHTTPClient(httpClient);
Requests may be logged by passing a function as the third constructor argument.
constDeskproClient=require('@deskpro/deskpro-api-client-javascript');constlogger=console.log;constclient=newDeskproClient('http://deskpro.company.com',null,logger);// Or use the setter method.// client.setLogger(logger);
Authenticating
Many API methods require authentication. Set the ID of the user to authenticate with and either the auth "key" or "token".
Each of the methods get, post, put, delete, batch, and request returns an object with the properties data, meta, and linked.
Browser Usage
The client library may be used in the browser as well as Node. The library may be installed using npm or embedded in a document using the unpkg.com CDN.
const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const client = new DeskproClient('http://deskpro.company.com');
// Use the setAuthKey method to authenticate using a key.
const personId = 1;
const authKey = 'dev-admin-code';
client.setAuthKey(personId, authKey);
// Use the setAuthToken method to authenticate using a token.
personId = 1;
authToken = 'AWJ2BQ7WG589PQ6S862TCGY4';
client.setAuthToken(personId, authToken);
const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const client = new DeskproClient('http://deskpro.company.com');
client.setAuthKey(1, 'dev-admin-code');
client.get('/articles')
.then((resp) => {
console.log(resp.data);
})
.catch((err) => {
console.error(err.message);
});
// Parameters are interpolated into the provided URL.
// The next request will be made to "/articles/101?limit=25".
const params = {
id: 101,
limit: 25
};
client.get('/articles/{id}', params);
const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const client = new DeskproClient('http://deskpro.company.com');
client.setAuthKey(1, 'dev-admin-code');
const body = {
title: 'Mary Had a Little Lamb',
content: 'Whose fleece was white as snow.'
};
client.post('/articles', body)
.then((resp) => {
console.log(resp.data);
})
.catch((err) => {
console.error(err.message);
});
const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const client = new DeskproClient('http://deskpro.company.com');
client.setAuthKey(1, 'dev-admin-code');
const body = {
title: 'Mary Had a Little Lamb',
content: 'Whose fleece was white as snow.'
};
const params = {
id: 101
};
client.put('/articles/{id}', body, params)
.then((resp) => {
console.log(resp.data);
})
.catch((err) => {
console.error(err.message);
});
const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const client = new DeskproClient('http://deskpro.company.com');
client.setAuthKey(1, 'dev-admin-code');
const body = {
title: 'Mary Had a Little Lamb',
content: 'Whose fleece was white as snow.'
};
const params = {
id: 101
};
const headers = {
'X-Custom-Value': 'some value'
}
client.request('PUT', '/articles/{id}', body, params, headers)
.then((resp) => {
console.log(resp.data);
})
.catch((err) => {
console.error(err.message);
});
const DeskproClient = require('@deskpro/deskpro-api-client-javascript');
const client = new DeskproClient('http://deskpro.company.com');
client.setAuthKey(1, 'dev-admin-code');
// Send these headers and values with each request made to the API.
client.setDefaultHeaders({
'X-Custom-Value': 'some value'
});