Programmatic instance management
The Python and TypeScript scripts on this page demonstrate how the FluidStack API can be used to manage instances programmatically, with or without using one of our SDKs.
Prerequisites
- An API key
- A public/private key pair
- (for Python) Basic familiarity with Python; Python 3+ installed
- (for TypeScript) Basic familiarity with TypeScript; Node 18+ installed; a way to run TypeScript code locally
Overview of example scripts
Without SDK
With SDK
Add an SSH key
The script reads the public key located at ~/.ssh/id_rsa.pub
, then sends a request to the Add an SSH key endpoint to add an SSH key. This key will be named either python_ssh_key
or typescript_ssh_key
and is a copy of the public key that was read.
Warning
- If you already have an SSH key on your FluidStack account named
python_ssh_key
ortypescript_ssh_key
, change the script to use a different, unique name. Alternatively, delete the existing key. - If your public key is in a different location or filename, make sure to update that part of the script.
Create an instance
The script creates an instance by sending a request to the Create a new instance endpoint.
The API key is sent in the request as a header. A custom name for the instance, the GPU type, and the name of the SSH key to use for accessing the instance are sent in the JSON body of the request.
Check the instance status
Initially, the instance’s status
is pending
. The script repeatedly checks this status
until it is running
by sending requests to the Fetch a single user instances endpoint and parsing the response.
This status-checking loop stops once the instance’s status
returns as running
, or if its status
indicates that it has been terminated
or is otherwise unable to run.
Example scripts
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.
Python
Python SDK
TypeScript
TypeScript SDK
Before you run this script, replace the api_key
value on line 7 with your own. You must also install the requests
library into your environment.
Cleanup
The script adds and deletes an SSH key and instance on the account associated with the provided API key.
In case you ran into any issues with the script, confirm that the instance was deleted. If it was not deleted, stop or delete it manually to avoid incurring charges.
If the SSH key on the account created by the script was not deleted, you must remove it manually before you can run the script again.
What’s next?
- Learn more about how to use the Python SDK.
- View the full API reference.
- Find out more about how to use SSH, including how to log into an instance.