Skip to content

Eval

The Eval API lets you run arbitrary JavaScript/TypeScript, with full access to the Val Town runtime.

Vals created via this API will not appear in the authenticated user’s workspace but they will be created and can be accessed (and moved into the user’s workspace) via their direct link on the frontend: https://val.town/v/{handle}.{val}.

GET /v1/eval/{expression}

One of the more fun parts of the Eval API is that you can explore it fully in the URL bar. For the following examples, click on the URLs to test them out yourself.

You can evaluate simple expression, like https://api.val.town/v1/eval/1+1 which returns 2.

You can compute with references to public vals, like https://api.val.town/v1/eval/@stevekrouse.example1+1.

POST /v1/eval

Pass the code expression to be evaluated via a JSON-encoded body, like so:

ExampleRun in Val Town ↗
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export let evalPost1 = fetchJSON(
"https://api.val.town/v1/eval",
{
method: "POST",
body: JSON.stringify({ code: "1+1" }),
},
);

You can also pass arguments if the code parameter represents a function:

ExampleRun in Val Town ↗
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export let evalPost2 = fetchJSON(
"https://api.val.town/v1/eval",
{
method: "POST",
body: JSON.stringify({ code: "(a,b) => a+b", args: [1, 2] }),
},
);