AXIOS API

The system can use the curl library to call third-party APIs, and now, starting with TAS 5.7, the axios library.

Advantages of using AXIOS over CURL

  1. Clearer writing
    Axios uses a simple and readable notation (e.g. axios.get(url) instead of multiple setOpt calls).
  2. Less code
    For common operations (GET, POST, PATCH…) there is no need to manually set the request type, headers, or body formatting.
  3. Better response handling
    Axios automatically converts responses to JSON (if Content-Type matches) and provides access to res.data , res.status , etc.
  4. Easier maintenance and readability for others
    Consultants and developers will understand what the code does more quickly because the structure matches common notation in modern JavaScript.

❗ When can't AXIOS be used?

  1. Axios only works for HTTP/HTTPS requests . It cannot be used for other protocols such as:
    1. SMTP (e.g. sending emails)
    2. FTP
    3. IMAP/POP3

In these cases, you should continue to use the curl library.

🔧 axios usage options

Axios supports all common HTTP methods:

GET

Used to retrieve data from the API (e.g. a list of items).

axios.getAxios().get('https://api.example.com/items');

POST

Used to send data to the server (e.g. creating a new item).

axios.getAxios().post('https://api.example.com/items', {
name: 'Nová položka'
});

PUT

Used to fully update an existing item.

axios.getAxios().put('https://api.example.com/items/123', {
name: 'Aktualizovaná položka'
});

PATCH

Used to partially update an item (e.g. just one field).

axios.getAxios().patch('https://api.example.com/items/123', {
status: 'aktivní'
});

DELETE

Used to delete an item.

axios.getAxios().delete('https://api.example.com/items/123');

request – general call (e.g. for PATCH, when you need your own method)

axios.getAxios().request({
method: 'patch',
url: 'https://api.example.com/items/123',
data: { status: 'hotovo' }
});

requestRaw – general call (possibility of getting header, status, statusText, data)

Available since version v5.7.37

Usage example

const config = {
method: "get",
url: `https://api.example.com/items/123`}

const request = axios.getAxios().requestRaw(config);

Typical answer

{
"data": {
// Obsah vrácený serverem (např. JSON data)
},
"status": 200,
"statusText": "OK",
"headers": {
"content-type": "application/json",
"date": "Mon, 22 Jul 2025 09:44:24 GMT"
}
}

📤 Sending a single file ( sendFile )

const file = lib.getFileContents('/cesta/k/souboru.txt');

axios.getAxios().sendFile('https://api.example.com/upload', file, {
headers: {
'Content-Type': 'application/octet-stream'
}
});

📤 Sending a single file ( sendFileRaw ) (possibility of getting header, status, statusText, data)

Available since version v5.7.37
const file = lib.getFileContents('/cesta/k/souboru.txt');

axios.getAxios().sendFileRaw('https://api.example.com/upload', file, {
headers: {
'Content-Type': 'application/octet-stream'
}
});

Typical answer

{
"data": {
// Obsah vrácený serverem (např. JSON data)
},
"status": 200,
"statusText": "OK",
"headers": {
"content-type": "application/json",
"date": "Mon, 22 Jul 2025 09:44:24 GMT"
}
}

Example of sending a file to the system with an API key

const URL = 'https://api.example.com/123';
const ocpKey = 'xxxxx';
const documentName = vars['attachedInvPDF'].getValue();
const invNumber = vars['internalNumber'].getValue() + path.extname(documentName[0]);
const dmsEntity = storage.getDmsEntity(documentName);
const path = exportDmsFile(dmsEntity, invNumber)

const result = axios.getAxios({
headers: {
'Ocp-Apim-Subscription-Key': ocpKey
}
}).sendFileRaw(URL, path);

Sample answer

{
"data": "",
"headers": {
"content-length": "0",
"operation-location": "https://api.example.com/123",
"x-envoy-upstream-service-time": "109",
"apim-request-id": "xxx-abfd-xx-a820-xxxx",
"strict-transport-security": "max-age=31536000; includeSubDomains; preload",
"x-content-type-options": "nosniff",
"x-ms-region": "West Europe",
"date": "Thu, 24 Jul 2025 09:43:36 GMT"
},
"status": 202,
"statusText": "Accepted"
}

📤 Sending multiple files ( sendFiles/sendFilesRaw ) via FormData

Since version 5.7.37 - you can also use sendFilesRaw, which returns an object similar to the Raw methods mentioned above.
const FormData = require('form-data');
const form = new FormData();

form.append('file1', lib.getFileContents('/cesta/k/soubor1.jpg'));
form.append('file2', lib.getFileContents('/cesta/k/soubor2.jpg'));

axios.getAxios().sendFiles('https://api.example.com/upload', form, {
headers: form.getHeaders()
});

📝 Sending form data ( sendFormData )

const FormData = require('form-data');
const form = new FormData();

form.append('name', 'Test');
form.append('email', 'test@example.com');

axios.getAxios().post('https://api.example.com/form', form, {
headers: form.getHeaders()
});

➕ Adding a file to FormData ( addFileToFormData )

form.append('file', lib.getFileContents('/cesta/k/souboru.pdf'));

➕ Adding a text field to FormData ( addFieldToFormData )

form.append('description', 'Nahrávám soubor k článku');

🔄 Processing response

Axios returns the response as an object, e.g.:

const response = axios.getAxios().get(url);
proc.warn(response.data); // data z API

AXIOS features

  • Timeout - default timeout is 0, in this case it is timeout calculations (default 120 s)
  • Throw error - in case of a response status other than 200, the corresponding error is thrown.
  • Buffer.from
AXIOS natively uses JSON as a format for transferring data. If you need to send e.g. XML, plainText, etc., you need to tell AXIOS not to transform the data. Buffer.from is used for this.

This function converts a regular text string ( string ) to binary data ( Buffer ) in UTF-8 encoding. It is used to properly send XML or other text data via AXIOS.

Example:

 const xml = '<soap>...</soap>';
const payload = Buffer.from(xml, 'utf-8');

AXIOS usage examples

Using GET to retrieve data from the CNB
function axiosGetDataByURL(requestURL) {
try {
const response = axios.getAxios().get(requestURL);
return response;
} catch (error) {
return {
error: true,
fullError: error,
statusCode: error?.response?.status,
statusText: error?.response?.statusText
};
}
}

try {
const cnbExchangeRatesURL = `https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/denni_kurz.xml`;
const requestResponse = axiosGetDataByURL(cnbExchangeRatesURL);

if (requestResponse?.error) {
throw new Error(`[CNB][Response][Error] ${requestResponse?.statusText}`, {
cause: requestResponse?.fullError
});
}

proc.warn(`[CNB][ResponseData]`, { requestResponse });

} catch (error) {
proc.warn(
`Během stažení informací z ČNB nastal problém: ${error?.message || `Neznámá chyba`} (rozklikněte log pro další detaily)`,
{ cause: error?.cause }
);
}

Using PATCH to update data on docs.teamassistant.app
 function patchMainBody(articleId, newBody) {
const url = `https://api.helpdocs.io/v1/article/${articleId}`;
const payload = { body: newBody };

const requester = axios.getAxios({
timeout: 10000,
headers: {
'Authorization': 'Bearer xxxxxxxx'
}
});

return requester.patch(url, payload);
}

🔍Differences between using CURL and AXIOS - examples

Calling CNB rates

CURL

function rateCNB(exchangeRateDate, currency, reportingCurrency) {
let formatDate = lib.format(exchangeRateDate, "dmY");

curl.start();
curl.setOpt('CUSTOMREQUEST', 'GET');
curl.setOpt('FOLLOWLOCATION', true);
curl.setOpt('FAILONERROR', false);
curl.setOpt('SSL_VERIFYPEER', false);
curl.setOpt('SSL_VERIFYHOST', false);
curl.setOpt('TIMEOUT', 30);
curl.setOpt('HTTPHEADER', [
'Content-Type: application/json',
'Accept: application/json'
]);

curl.setOpt('URL', `https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/denni_kurz.txt?date=${formatDate}`);
var crossRate = curl.perform();
//search for specific currency
try {
const body = crossRate.data;
...

AXIOS

function rateCNB(exchangeRateDate, currency, reportingCurrency) {
let formatDate = lib.format(exchangeRateDate, "dmY");

const url = `https://www.cnb.cz/cs/financni-trhy/devizovy-trh/kurzy-devizoveho-trhu/kurzy-devizoveho-trhu/denni_kurz.txt?date=${formatDate}`;

const body = axios.getAxios().get(url);
//search for specific currency
try {
const rows = body.split('\n');
...

Frantisek Brych Updated by Frantisek Brych

Translations

Contact

Syca (opens in a new tab)

Powered by HelpDocs (opens in a new tab)