Foundry API quickstart

The Foundry API is currently in an experimental release. The endpoints documented here may be subject to breaking changes in the future.

With the Foundry API, you can programmatically place spot instance bids to dynamically scale your compute. A full version of this API will ship in the coming months, but this Jupyter notebook (replicated below) will help you get started.

Foundry API Demo

import http.client
import json

API_URL = "api.mlfoundry.com"
#First you'll need to login to grab your access token. You can do this by running the following code block.
conn = http.client.HTTPSConnection(API_URL)

data = {'email': 'YOUR_EMAIL', 'password': 'YOUR_PASSWORD'}
payload = json.dumps(data)
conn.request("POST", "/login", payload)
res = conn.getresponse()
data = json.loads(res.read().decode("utf-8"))

access_token = data["access_token"]
def foundryReq(path, verb, payload):
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + access_token
    }
    if payload:
        payload = json.dumps(payload)
    conn = http.client.HTTPSConnection(API_URL)
    conn.request(verb, path, headers=headers, body=payload)
    res = conn.getresponse()
    data = res.read().decode("utf-8")
    return json.loads(data)

def foundryGet(path):
    return foundryReq(path, "GET", None)

def foundryPost(path, payload):
    return foundryReq(path, "POST", payload)

def foundryDel(path):
    return foundryReq(path, "DELETE", None)
user_id = foundryGet("/users/")["id"]
projects = foundryGet("/users/" + str(user_id) + "/projects")

#Assuming the first project
project_id = projects[0]["id"]
#Grab all your instances
all_instances = foundryGet("/projects/" + str(project_id) + "/all_instances")
#Grab available auctions
auctions = foundryGet("/projects/" + str(project_id) + "/spot-auctions/auctions")
#Grab your ssh keys
ssh_keys = foundryGet("/projects/" + str(project_id) + "/ssh_keys")

#Using the first ssh_key for now
ssh_key_id = ssh_keys[0]["id"]
#Place a bid on the first auction
auction = auctions[0]

order_name = "demo-test-bid"
bid_price_cents = 1000
num_instances = 1
startup_script = ""

bid_payload = {
    "cluster_id": auction["cluster_id"],
    "instance_quantity": num_instances,
    "instance_type_id": auction["instance_type_id"],
    "limit_price_cents": bid_price_cents,
    "order_name": order_name,
    "project_id": project_id,
    "ssh_key_ids": [ssh_key_id],
    "startup_script": startup_script,
    "user_id": user_id
}

bid = foundryPost("/projects/" + str(project_id) + "/spot-auctions/bids", bid_payload)
#Cancel a bid

bid_id = bid["id"]
foundryDel("/projects/" + str(project_id) + "/spot-auctions/bids/" + str(bid_id))
#Grab all your bids
bids = foundryGet("/projects/" + str(project_id) + "/spot-auctions/bids")

Last updated