Preview
Sign inSign up
Getting startedExamples

Getting started

Examples

Calling the API from a shell, JavaScript and Python.

Shell

For anything longer than a line, send the body through a quoted heredoc so quotes, $ and backticks in the source reach the API untouched. This one pipes the result through jq:

shell
curl -sS https://api.cons.stneto.dev/v1/eval \
  -H "Authorization: Bearer $CONS_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<'CONS_EOF' | jq '.results[].value'
{"source": "(define (fib n)\n  (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2)))))\n(fib 20)"}
CONS_EOF

JavaScript

With fetch, in Node 18+ or any runtime that has it:

eval.mjs
const res = await fetch("https://api.cons.stneto.dev/v1/eval", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.CONS_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ source: "(map (lambda (x) (* x x)) '(1 2 3))" }),
});
const { status, results, error } = await res.json();
console.log(status, results.at(-1)?.json); // ok [1, 4, 9]

A helper that tells request errors from program errors:

evaluate.mjs
async function evaluate(source) {
  const res = await fetch("https://api.cons.stneto.dev/v1/eval", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.CONS_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ source }),
  });
  const body = await res.json();

  // 4xx and 5xx: the request was turned away.
  if (!res.ok) {
    const retryAfter = res.headers.get("Retry-After");
    throw new Error(`${body.error.type}: ${body.error.message}` +
      (retryAfter ? ` (retry in ${retryAfter} s)` : ""));
  }

  // 200: the program ran. status says how it ended.
  if (body.status !== "ok") {
    const { type, message, line, col } = body.error;
    throw new Error(`${type} at ${line}:${col}: ${message}`);
  }
  return body.results.map((r) => r.json);
}

Python

With requests:

eval.py
import os, requests

res = requests.post(
    "https://api.cons.stneto.dev/v1/eval",
    headers={"Authorization": f"Bearer {os.environ['CONS_KEY']}"},
    json={"source": '(string-append "hello, " "world")', "timeout_ms": 2000},
)
body = res.json()
print(body["status"], body["results"][-1]["json"])  # ok hello, world
esc