tencent cloud

NodeJS SDK Log Upload
Last updated: 2025-11-07 17:40:38
NodeJS SDK Log Upload
Last updated: 2025-11-07 17:40:38
This article introduces how to quickly get started with the NodeJS SDK of Cloud Log Service (CLS) to implement log upload. For more details on SDK usage, visit the code repository tencentcloud-cls-sdk-js.

Prerequisites

Create and obtain TencentCloud API key information accessKeyId and accessKey. For key information acquisition, please visit API Key Management.
Ensure the associated account has appropriate SDK log upload permission.

Preparing the Development Environment

Before accessing, please first download and install Node.js. For the download link and related operations, see the Node.js official website.
For CentOS 7, run the following command to install the development environment.
# Install EPEL repository
sudo yum install epel-release -y
# Install Node.js and npm
sudo yum install nodejs npm -y

Installing the NodeJS SDK

1. Create and enter the project directory.
2. Install the SDK via npm.
npm install
npm install tencentcloud-cls-sdk-js

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Domain information. For completion, see the domain name in the API upload log Tab under available region.
secretId
String
Yes
TencentCloud API key information. For key information acquisition, visit API key management. Ensure the associated account has appropriate SDK log upload permission.
secretKey
String
Yes
TencentCloud API key information. For key information acquisition, visit API key management. Ensure the associated account has appropriate SDK log upload permission.
sourceIp
String
No
Source IP address.
retry_times
integer
Yes
Maximum number of retries
topicID
String
Yes
Log topic ID information.

Log Upload Example Code

The following code shows how to complete log upload by calling the SDK, using the NodeJS SDK as an example.
It is not recommended to store TencentCloud API key information in plaintext in project code. You can dynamically obtain the API key information through environment variables. For detailed operations, please refer to configure environment variables.
const { AsyncClient, LogItem, LogGroup, Content, PutLogsRequest } = require('tencentcloud-cls-sdk-js');

// CLS log topic ID; required parameter
let topicID = "591XXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // replace with your log topic ID

let client = new AsyncClient({
// Fill in domain information, see the domain name in the Log upload via API Tab: https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.E5.9F.9F.E5.90.8D
endpoint: "ap-xxxxxx.cls.tencentcs.com",
// Fill in TencentCloud API key information. For key information acquisition, please visit: https://console.tencentcloud.com/cam/capi
// Ensure the associated account has appropriate log upload permissions. Permission configuration guide: https://www.tencentcloud.com/document/product/614/68374?from_cn_redirect=1#.E4.BD.BF.E7.94.A8-api-.E4.B8.8A.E4.BC.A0.E6.95.B0.E6.8D.AE
// This example retrieves from environmental variable. Environment variable configuration guide: https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1
secretId: process.env.TENCENTCLOUD_SECRET_ID,
secretKey: process.env.TENCENTCLOUD_SECRET_KEY,
// source IP address: optional parameter, leave blank for automatic filling with local IP address
sourceIp: "127.XX.XX.1",
// retry count: required parameter
retry_times: 10,
});

async function sendLog() {
let item = new LogItem();
item.pushBack(new Content("__CONTENT__", "This is the log content to be sent")); // Change to the log content you want to send
item.setTime(Math.floor(Date.now() / 1000));

let loggroup = new LogGroup();
loggroup.addLogs(item);
let request = new PutLogsRequest(topicID, loggroup);

try {
let data = await client.PutLogs(request);
console.log("Log sent successfully:", data);
} catch (error) {
console.error("Failed to send log:", error);
}
}

call the send log function
sendLog();

Conclusions

Through these steps, you can quickly use the Tencent Cloud CLS NodeJS SDK to complete log upload. If you encounter any issues, please contact us to get help.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback