How to use the TypeScript SDK

Prerequisites

  • An API key
  • Familiarity with command-line tools
  • Basic familiarity with TypeScript and Node.js; Node 18+ installed

Installation

Our TypeScript SDK is hosted at NPM.

Use npm or yarn to install it to your project:

$npm install fluidstack
$yarn add fluidstack

Instantiate a client

Import FluidStackApiClient and instantiate a TypeScript client with your API key:

Example
1import { FluidStackApiClient } from 'fluidstack';
2
3const client = new FluidStackApiClient({
4 apiKey: '<your_api_key>'
5});

Now you can use the client to consume the API from your TypeScript application.

Warning

Avoid placing your API key in any file that might be shared with others. For information on using a .env file instead of adding the API key directly into a file, see: API Overview - Secure use of your API key.

Use the FluidStack TypeScript client in your app

The FluidStack TypeScript client simplifies making API requests. It stores the API key that you used to instantiate it, and it already knows our API server’s base URL, so you can omit those details in your requests.

The SDK also provides code hints, type hints, parameter information, and other useful functions to speed up development.

For example, compare the tabs below:

1client.instances.create(
2 gpuType='RTX_A6000_48GB',
3 sshKey='mySSHKey'
4)

You can see that instead of using fetch or a similar module to send an HTTP request and including the endpoint path and headers each time, the FluidStack TypeScript client simplifies the request for you.

Call endpoints

Requests to API endpoints are implemented in the SDK as methods of the client. For example, the GET /instances endpoint is implemented like this:

Get the list of instances with the SDK
1client.instances.list()

In TypeScript/JavaScript, you must handle asynchronous operations such as HTTP requests explicitly using async/await or promises. To use async/await, wrap the method call in an async function and await the result, then call the function:

Use async/await to get the list of instances
1async function listInstances() {
2 const instances = await client.instances.list();
3}
4
5listInstances();

The code shown above does not do anything with the response from the endpoint. It is up to you to handle the response, including error handling.

For example, you could simply print the entire response to the terminal:

Print the entire response
1async function listInstances() {
2 try {
3 const instances = await client.instances.list();
4 console.log(instances);
5 } catch (error) {
6 console.error('An error occurred:', error);
7 }
8}
9
10// Call the async function
11listInstances();

Or you could loop through the list and print only the name and status for each instance:

Print each instance name and status
1async function listInstances() {
2 const instances = await client.instances.list();
3 instances.forEach(instance => {
4 console.log(`Instance: ${instance.name}, Status: ${instance.status}`);
5 });
6}
7
8listInstances();

See Programmatic Instance Management for a more detailed tutorial on using TypeScript, Python, and their respective SDKs.

Instances

1 client.instances.list()

Parameters: None.

SSH Keys

1 client.sshKeys.list()

Parameters: None.

List available configurations and operating system templates

1client.configurations.list()

Parameters: None.