Guides

Code examples

These examples use direct HTTP requests against the public API. Keep API keys on trusted servers and load them from environment variables.

Reserve, upload, and validate a model bundle

Python

import os
from pathlib import Path

import requests

BASE_URL = "https://api.flashml.dev"
API_KEY = os.environ["FLASHML_API_KEY"]
HEADERS = {"Authorization": f"Bearer {API_KEY}"}

bundle = Path("sentiment-classifier.tar.gz")

reservation = requests.post(
    f"{BASE_URL}/v1/models/uploads",
    headers={**HEADERS, "Content-Type": "application/json"},
    json={
        "filename": bundle.name,
        "modality": "text",
        "task": "classification",
        "size_bytes": bundle.stat().st_size,
    },
    timeout=30,
)
reservation.raise_for_status()
upload = reservation.json()

with bundle.open("rb") as file:
    put = requests.put(upload["upload_url"], data=file, timeout=300)
put.raise_for_status()

completed = requests.post(
    f"{BASE_URL}/v1/models/uploads/{upload['upload_id']}/complete",
    headers=HEADERS,
    timeout=300,
)
completed.raise_for_status()
model = completed.json()["model"]
print(model["id"], model["status"])

TypeScript

import { createReadStream, statSync } from "node:fs";
import { basename } from "node:path";

const BASE_URL = "https://api.flashml.dev";
const API_KEY = process.env.FLASHML_API_KEY!;
const bundlePath = "sentiment-classifier.tar.gz";

const reservation = await fetch(`${BASE_URL}/v1/models/uploads`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    filename: basename(bundlePath),
    modality: "text",
    task: "classification",
    size_bytes: statSync(bundlePath).size,
  }),
});

if (!reservation.ok) throw new Error(await reservation.text());
const upload = await reservation.json();

const put = await fetch(upload.upload_url, {
  method: "PUT",
  body: createReadStream(bundlePath) as unknown as BodyInit,
});

if (!put.ok) throw new Error(await put.text());

const completed = await fetch(
  `${BASE_URL}/v1/models/uploads/${upload.upload_id}/complete`,
  {
    method: "POST",
    headers: { Authorization: `Bearer ${API_KEY}` },
  }
);

if (!completed.ok) throw new Error(await completed.text());
const { model } = await completed.json();
console.log(model.id, model.status);

Run text inference

Python

import os

import requests

BASE_URL = "https://api.flashml.dev"
API_KEY = os.environ["FLASHML_API_KEY"]
MODEL_ID = os.environ["FLASHML_MODEL_ID"]

response = requests.post(
    f"{BASE_URL}/v1/models/{MODEL_ID}/infer",
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
    },
    json={"text": "The checkout flow was fast and reliable."},
    timeout=60,
)
response.raise_for_status()
print(response.json())

TypeScript

const BASE_URL = "https://api.flashml.dev";
const API_KEY = process.env.FLASHML_API_KEY!;
const MODEL_ID = process.env.FLASHML_MODEL_ID!;

const response = await fetch(`${BASE_URL}/v1/models/${MODEL_ID}/infer`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    text: "The checkout flow was fast and reliable.",
  }),
});

if (!response.ok) throw new Error(await response.text());
console.log(await response.json());

List validated models

Python

import os

import requests

BASE_URL = "https://api.flashml.dev"
API_KEY = os.environ["FLASHML_API_KEY"]

response = requests.get(
    f"{BASE_URL}/v1/models",
    headers={"Authorization": f"Bearer {API_KEY}"},
    timeout=30,
)
response.raise_for_status()

for model in response.json()["models"]:
    print(model["id"], model["name"], model["version"], model["status"])

TypeScript

const BASE_URL = "https://api.flashml.dev";
const API_KEY = process.env.FLASHML_API_KEY!;

const response = await fetch(`${BASE_URL}/v1/models`, {
  headers: { Authorization: `Bearer ${API_KEY}` },
});

if (!response.ok) throw new Error(await response.text());
const { models } = await response.json();

for (const model of models) {
  console.log(model.id, model.name, model.version, model.status);
}

Client placement

Run these calls from server-side services, jobs, or trusted notebooks. Do not ship FlashML API keys to browsers or customer devices.