Creating Container through Node.js

amarnath

New Member
Dec 26, 2023
1
0
1
const axios = require('axios');
const proxmoxBaseUrl = 'https://192.168.1.3:8006/api2/json';
const proxmoxNode = 'proxmox';
const proxmoxUser = 'root@pam';
const proxmoxPassword = 'amarnath';
const getAuthToken = async () => {
try {
console.log('Step 1: Requesting authentication token...');

const response = await axios.post(
`${proxmoxBaseUrl}/access/ticket`,
{
username: proxmoxUser,
password: proxmoxPassword,
},
{ httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }) }
);
const authToken = response.data.data.ticket;

console.log('Step 2: Authentication token received successfully:', authToken);

return authToken;
} catch (error) {
console.error('Error getting authentication token:', error.message);
throw error;
}
};
const createContainer = async (vmid) => {
try {
console.log('Step 3: Getting authentication token...');
const authToken = await getAuthToken();
console.log('Step 4: Authentication token obtained:', authToken);
console.log('Step 5: Creating container...');
const createResponse = await axios.post(
`${proxmoxBaseUrl}/nodes/${proxmoxNode}/qemu`,
{
vmid: vmid,
ostype: 'l26',
// Add other container configuration options here
},
{
headers: {
Cookie: `PVEAuthCookie=${authToken}`,
},
httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }),
}
);
console.log('Step 6: Container created successfully. Response:', createResponse.data);
} catch (error) {
console.error('Error creating container:', error.message, 'Response:', error.response?.data);
}
};
// Example usage
createContainer(500);


my terminal output

PS D:\Study\React_Project\back_end> node server.js
Step 3: Getting authentication token...
Step 1: Requesting authentication token...
(node:5908) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5908) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
Server is running on port 5000
MongoDB database connection established successfully
Step 2: Authentication token received successfully: PVE:root@pam:658A8FB0::GCKL7KW8vkCHdPer3Hfsy3Tf3qpyfGNuk1kC5FKAykzKZ+YhDnybGa1QYMLobvnzQDf54+gWsWFHZl6dpkBsQhgNnXob9a8K1zdsUty/o4ZgQIvTvvHogF1ZIy9FPsEQqMEfBJZWWf5Z8KWBxx+KHiUHeZmdDVXKDxC3a/nd7yOHkCEV8IVxfs/QsjkHNshNjGQ5/00yOGDAd/g+3kdM0aeud1riZm+C/g/TTlLq9ePYmBqonqg/em+KrlFNC9OoTUeiO9TI0A3mxWW4eWAbDLgKl2RN9u6sol9myXk+rdhjx/osBvuHErvxqI58ozIJD/jNeNS56EmX1n/sstqaHA==
Step 4: Authentication token obtained: PVE:root@pam:658A8FB0::GCKL7KW8vkCHdPer3Hfsy3Tf3qpyfGNuk1kC5FKAykzKZ+YhDnybGa1QYMLobvnzQDf54+gWsWFHZl6dpkBsQhgNnXob9a8K1zdsUty/o4ZgQIvTvvHogF1ZIy9FPsEQqMEfBJZWWf5Z8KWBxx+KHiUHeZmdDVXKDxC3a/nd7yOHkCEV8IVxfs/QsjkHNshNjGQ5/00yOGDAd/g+3kdM0aeud1riZm+C/g/TTlLq9ePYmBqonqg/em+KrlFNC9OoTUeiO9TI0A3mxWW4eWAbDLgKl2RN9u6sol9myXk+rdhjx/osBvuHErvxqI58ozIJD/jNeNS56EmX1n/sstqaHA==
Step 5: Creating container...
Error creating container: Request failed with status code 401 Response:

if i userd Python it works and create container on proxmox
my python code

from proxmoxer import ProxmoxAPI
proxmox = ProxmoxAPI(
"192.168.1.3", user="root@pam", password="amarnath", verify_ssl=False
)
node = "proxmox"
vmid = 300
try:
create_response = proxmox.nodes(node).qemu.create(
vmid=vmid,
ostype="l26", # Adjust the OSType as needed
)
print(f"Container {vmid} created successfully.")
except Exception as e:
print(f"Error creating container: {str(e)}")
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!