- Home
- API and CLI Reference
- API Basics
- NodeJS Example
NodeJS Example
What is NodeJS?
NodeJS serves as a JavaScript runtime enabling code execution outside the browser, often employed for crafting efficient web servers and network applications. Notably, the Nexus server is implemented using NodeJS.
Here's a basic example of how to use NodeJS to interact with the xcware API:
To interact with the xcware API, you'll need to ensure that you're sending POST requests with JSON data and expect JSON-structured data in response.
Request Headers
Now that we've laid down the groundwork, let's delve into the essential headers needed to construct a valid POST request for the xcware API.
x-access-account
This header denotes the account ID and is required for any request. You can find your account ID in the "Actions" menu situated at the top right corner of the Nexus interface.
x-access-authorization
This header signifies the access key linked to the roles and policies within the specified account, and it's a requisite for all requests. Please obtain your access key from your system administrator.
x-secret-key
This header indicates the secret key tied to the specified account, and it's mandatory for all requests. Please obtain your secret key from your system administrator.
x-api
This header defines the name of the API containing the commands and is obligatory. Refer to the API Builder for potential API names.
x-api-command
This header specifies the name of the function to be executed and is mandatory. Refer to the API Builder for potential command names.
Data
The "const data =" section in the code signifies the JSON structured input data transmitted to the API task and is mandatory. For potential data input options, refer to the API Builder.
NodeJS serves as a JavaScript runtime enabling code execution outside the browser, often employed for crafting efficient web servers and network applications. Notably, the Nexus server is implemented using NodeJS.
Here's a basic example of how to use NodeJS to interact with the xcware API:
To interact with the xcware API, you'll need to ensure that you're sending POST requests with JSON data and expect JSON-structured data in response.
Copy
import fetch from 'node-fetch'; //<-- npm install node-fetch
/*
Using the following code as a workaround for self-signed certificate
errors is discouraged in production environments.
*/
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
//CREATE CONTAINER API CALL
const apiUrl = "https://your_nexus_server_or_ip/api/v3/"; //<-- replace
//JSON data to be sent
const data = {
"cloudid": "V7d41fdc53-xxxx-xxxx-xxxx-5c1fxxxx3cbc", //<-- replace
"name": "My Container",
"os": "cn-rocky-9",
"password": "test"
};
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-access-account": "YOUR_ACCOUNT_ID", //<-- replace
"x-access-authorization": "YOUR_ACCESS_KEY", //<-- replace
"x-secret-key": "YOUR_SECRET_KEY", //<-- replace
"x-api": "instances-cn",
"x-api-command": "create"
},
body: JSON.stringify(data),
};
fetch(apiUrl, requestOptions)
.then((response) => {
if (!response.ok) {
throw new Error("Connection error");
}
return response.json();
})
.then((data) => {
//process received JSON data
console.log(JSON.stringify(data, null, 2));
if (data.result == "OK") {
console.log("Container was created with the ID: "+data.id);
} else {
console.log("Error: " + data.message);
}
})
.catch((error) => {
console.log("Error: " + error);
});
Request Headers
Now that we've laid down the groundwork, let's delve into the essential headers needed to construct a valid POST request for the xcware API.
x-access-account
This header denotes the account ID and is required for any request. You can find your account ID in the "Actions" menu situated at the top right corner of the Nexus interface.
x-access-authorization
This header signifies the access key linked to the roles and policies within the specified account, and it's a requisite for all requests. Please obtain your access key from your system administrator.
x-secret-key
This header indicates the secret key tied to the specified account, and it's mandatory for all requests. Please obtain your secret key from your system administrator.
x-api
This header defines the name of the API containing the commands and is obligatory. Refer to the API Builder for potential API names.
x-api-command
This header specifies the name of the function to be executed and is mandatory. Refer to the API Builder for potential command names.
Data
The "const data =" section in the code signifies the JSON structured input data transmitted to the API task and is mandatory. For potential data input options, refer to the API Builder.
Get Started with API Builder