Javascript SDK (alpha release)
Hyperstack provides SDKs for Python, Javascript, Java, and Go, allowing developers to programmatically manage their cloud resources with ease. Similar to SDKs offered by other major cloud platforms, the Hyperstack SDKs enable you to integrate and automate interactions with cloud resources directly within your applications, covering services like virtual machines, Kubernetes clusters, and network configurations.
In this guide, you’ll find detailed instructions to use the Hyperstack SDK for Javascript and Typescript. By following these steps, you can streamline cloud operations and management, bringing industry-standard practices directly into your codebase.
To view more examples and resources:
- Check out our public Javascript GitHub repository here.
- Check out our public Typescript GitHub repository here.
The Hyperstack SDKs are currently in alpha, and some features may still be under development. Exercise caution when deploying production-level operations.
In this article
1. Prerequisites
Before you begin using any of the SDKs, ensure you have installed the programming language’s runtime environment on your system. You can download the required runtime from the official websites:
Or if you are using Typescript:
2. Installation
2.1 Via NPM (Recommended)
To install the Hyperstack SDKs, use one of the following commands:
- Javascript
- Typescript
npm install @nexgencloud/hyperstack-sdk-javascript
npm install @nexgencloud/hyperstack-sdk-typescript
2.2 Via GitHub
Alternatively, you can clone the repository from GitHub:
- Javascript
- Typescript
npm install NexGenCloud/hyperstack-sdk-javascript --save
npm install NexGenCloud/hyperstack-sdk-typescript --save
2.3 Local development
Alternatively, you can develop the SDK locally by cloning the repository from GitHub:
- Javascript
- Typescript
git clone https://github.com/NexGenCloud/hyperstack-sdk-javascript.git
cd hyperstack-sdk-javascript
npm install
npm link
git clone https://github.com/NexGenCloud/hyperstack-sdk-typescript.git
cd hyperstack-sdk-typescript
npm install
npm link
3. Usage
After installing the SDK, you can start using it in your Javascript scripts.
For the TypeScript SDK, ensure that you set the HYPERSTACK_API_ADDRESS
environment variable to the following value: https://infrahub-api.nexgencloud.com/v1/
(ensure it ends with a slash).
This is a known issue and will be resolved in the upcoming release.
3.1 Authenticate
You can authenticate with the Hyperstack API using your API key.
To get your API key see instructions here
You can set the API key using the API client authentications :
- Javascript
- Typescript
const HyperstackApi = require('@nexgencloud/hyperstack-sdk-javascript');
// Initialize the ApiClient
const apiClient = new HyperstackApi.ApiClient();
apiClient.authentications["apiKey"].apiKey = process.env.HYPERSTACK_API_KEY;
// ...rest of code
// no ApiClient in Typescript, see alternative authentication method below
Alternatively you can set the API key after initializing a specific Api class.
- Javascript
- Typescript
const HyperstackApi = require('@nexgencloud/hyperstack-sdk-javascript');
const vmApi = new HyperstackApi.VirtualMachineApi();
vmApi.apiClient.authentications["apiKey"].apiKey = process.env.HYPERSTACK_API_KEY;
//rest of code
import { Configuration, VirtualMachineApi } from '@nexgencloud/hyperstack-sdk-typescript';
// Instantiate the API class with Configuration
const vmApi = new VirtualMachineApi(
new Configuration({
basePath: process.env.HYPERSTACK_API_ADDRESS,
apiKey: process.env.HYPERSTACK_API_KEY,
})
);
For the TypeScript SDK, ensure that you set the HYPERSTACK_API_ADDRESS
environment variable to the following value: https://infrahub-api.nexgencloud.com/v1/
(ensure it ends with a slash).
This is a known issue and will be resolved in the upcoming release.
3.2 Simple example
After setting the API key, you can use the following Python script to list all the virtual machines in your Hyperstack account. For this examples, make sure to set your API key as the environment variable HYPERSTACK_API_KEY
.
- Javascript
- Typescript
const HyperstackApi = require('@nexgencloud/hyperstack-sdk-javascript');
// Initialize the Virtual Machine API
const vmApi = new HyperstackApi.VirtualMachineApi();
vmApi.apiClient.authentications["apiKey"].apiKey = process.env.HYPERSTACK_API_KEY;
async function listVirtualMachines() {
try {
const response = await vmApi.listVirtualMachines(null);
console.log("Virtual Machines instances:", response.instances);
} catch (error) {
console.error("Error fetching virtual machines:", error);
}
}
// Execute the function
listVirtualMachines();
import type { Instances } from "@nexgencloud/hyperstack-sdk-typescript";
import { Configuration, VirtualMachineApi } from "@nexgencloud/hyperstack-sdk-typescript";
const configuration: Configuration = new Configuration({
basePath: process.env.HYPERSTACK_API_ADDRESS,
apiKey: process.env.HYPERSTACK_API_KEY,
});
// Instantiate the API class with Configuration
const vmApi = new VirtualMachineApi(configuration);
// Define a function to list virtual machines
async function listVirtualMachines(): Promise<void> {
try {
const response = await vmApi.listVirtualMachines();
const vmList: any = response.data;
const instances: Instances = vmList.instances;
console.log("Instances data:", instances);
} catch (error) {
console.error("Error fetching virtual machines:", error);
}
}
// Execute the function
listVirtualMachines();
For the TypeScript SDK, ensure that you set the HYPERSTACK_API_ADDRESS
environment variable to the following value: https://infrahub-api.nexgencloud.com/v1/
(ensure it ends with a slash).
This is a known issue and will be resolved in the upcoming release.
3.3 Advanced example
The following example demonstrates how to create a virtual machine with specific configurations. For this example, make sure to set your API key as the environment variable HYPERSTACK_API_KEY
and the Hyperstack API address as an environment variable HYPERSTACK_API_ADDRESS
.
- Javascript
- Typescript
const HyperstackApi = require("@nexgencloud/hyperstack-sdk-javascript");
// Initialize the ApiClient
const apiClient = new HyperstackSDK.ApiClient();
apiClient.authentications["apiKey"].apiKey = process.env.HYPERSTACK_API_KEY;
apiClient.basePath = process.env.HYPERSTACK_API_ADDRESS;
const vmApi = new HyperstackSDK.VirtualMachineApi(apiClient);
async function createVirtualMachine() {
try {
const payload = {
name: "my-vm-2",
environment_name: "default-CANADA-1",
key_name: "canada-key-prod-040624",
image_name: "Ubuntu Server 22.04 LTS R535 CUDA 12.2 with Docker",
flavor_name: "n1-cpu-medium",
count: 1,
assign_floating_ip: true,
labels: ["sdk-example"],
security_rules: [
{
direction: "ingress",
ethertype: "IPv4",
protocol: "tcp",
remote_ip_prefix: "0.0.0.0/0",
port_range_min: 22,
port_range_max: 22,
},
{
direction: "ingress",
ethertype: "IPv6",
protocol: "tcp",
remote_ip_prefix: "::/0",
port_range_min: 22,
port_range_max: 22,
},
],
};
const response = await vmApi.createVirtualMachines(payload);
const instanceDetails = response.instances[0];
const id = instanceDetails.id;
console.log(`Created virtual machine with ID: ${id}`);
} catch (error) {
console.error("An exception occurred while calling VirtualMachineApi.createVirtualMachines:", error);
}
}
// Execute the function
createVirtualMachine();
import {
Configuration,
VirtualMachineApi,
CreateInstancesPayload,
CreateInstancesResponse,
} from "@nexgencloud/hyperstack-sdk-typescript";
// Instantiate the API class with Configuration
const vmApi = new VirtualMachineApi(
new Configuration({
basePath: process.env.HYPERSTACK_API_ADDRESS,
apiKey: process.env.HYPERSTACK_API_KEY,
})
);
// Define a function to create a virtual machine
async function createVirtualMachine(): Promise<void> {
try {
const payload: CreateInstancesPayload = {
name: "my-vm-2",
environment_name: "default-CANADA-1",
key_name: "canada-key-prod-040624",
image_name: "Ubuntu Server 22.04 LTS R535 CUDA 12.2 with Docker",
flavor_name: "n1-cpu-medium",
count: 1,
assign_floating_ip: true,
labels: ["sdk-example"],
security_rules: [
{
direction: "ingress",
ethertype: "IPv4",
protocol: "tcp",
remote_ip_prefix: "0.0.0.0/0",
port_range_min: 22,
port_range_max: 22,
},
{
direction: "ingress",
ethertype: "IPv6",
protocol: "tcp",
remote_ip_prefix: "::/0",
port_range_min: 22,
port_range_max: 22,
},
],
};
const response = await vmApi.createVirtualMachines(payload);
const createVmResponse: CreateInstancesResponse = response.data;
if (createVmResponse.instances && createVmResponse.instances.length > 0) {
const instanceId = createVmResponse.instances[0].id;
console.log(`Created virtual machine with ID: ${instanceId}`);
} else {
console.error("No instances were created.");
}
} catch (error) {
console.error("An exception occurred while calling VirtualMachineApi.createVirtualMachines:", error);
}
}
// Execute the function
createVirtualMachine();
For the TypeScript SDK, ensure that you set the HYPERSTACK_API_ADDRESS
environment variable to the following value: https://infrahub-api.nexgencloud.com/v1/
(ensure it ends with a slash).
This is a known issue and will be resolved in the upcoming release.
4. Other resources
To explore more advanced usage and other examples, refer to our Javascript GitHub or Typescript GitHub