Skip to main content

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.
warning

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
  2. Installation
  3. Usage
  4. Other resources

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

To install the Hyperstack SDKs, use one of the following commands:

npm install @nexgencloud/hyperstack-sdk-javascript

2.2 Via GitHub

Alternatively, you can clone the repository from GitHub:

npm install NexGenCloud/hyperstack-sdk-javascript --save

2.3 Local development

Alternatively, you can develop the SDK locally by cloning the repository from GitHub:

git clone https://github.com/NexGenCloud/hyperstack-sdk-javascript.git
cd hyperstack-sdk-javascript
npm install
npm link

3. Usage

After installing the SDK, you can start using it in your Javascript scripts.

warning

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 :

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

Alternatively you can set the API key after initializing a specific Api class.

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

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.

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();

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.

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();

4. Other resources

To explore more advanced usage and other examples, refer to our Javascript GitHub or Typescript GitHub