onlinecompiler.io

Piston-compatible API

A Piston v2-compatible API at https://api.onlinecompiler.io/api/v2/piston, for apps built on the public Piston API: the same execute and runtimes endpoints, requests and responses, with your API key in the Authorization header.

Code written for Piston's v2 API runs here with two changes: point its base URL at https://api.onlinecompiler.io/api/v2/piston, and send your API key in the Authorization header. Requests and responses keep Piston's shape, including errors as {"message": "..."}.

GET /api/v2/piston/runtimes no key needed
POST /api/v2/piston/execute
Terminal
curl -X POST https://api.onlinecompiler.io/api/v2/piston/execute \
  -H "Authorization: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "language": "python",
    "version": "*",
    "files": [{ "name": "main.py", "content": "print(input())" }],
    "stdin": "hello"
  }'

Response (200)

Response
{
  "language": "python",
  "version": "3.14",
  "run": {
    "stdout": "hello\n",
    "stderr": "",
    "output": "hello\n",
    "code": 0,
    "signal": null,
    "message": null,
    "status": null,
    "cpu_time": null,
    "wall_time": 21,
    "memory": 9216000
  }
}

Differences from Piston

  • Piston's language names and aliases for our 46 languages work, such as py, c++ and csharp. Other languages answer 400 with "runtime is unknown".
  • version is accepted and ignored: programs run on the current version that /runtimes lists. run_timeout and run_cpu_time stop the run at the lower of the two, capped at 30 seconds; compiling does not count, and Java, Groovy and F# get a start-up allowance on top (Java (1 s), Groovy (1.5 s) and F# (2.5 s)). Compile limits and memory limits are ignored, and our memory limit applies.
  • run_memory_limit is ignored: a limit here would count the language's runtime, and a Java, Kotlin or F# program uses over 200 MB just to start. For a lower limit of your own, send memory_limit to the native run-code API.
  • The first entry in files is the program, in utf8, base64 or hex. Any further entries are input files, placed in the program's working directory under their name, so a program can read a CSV. They follow the input file limits: up to 20 files and 1 MB in total. A name may have folders, such as pkg/util.py, and a helper source file is compiled or loaded with the program as on the run endpoints.
  • args reach the program after its command, in order and exactly as sent: up to 32 strings and 4,096 bytes in all. sqlite-3, duckdb-1.5 and erlang-29 cannot take them, and a request that sends some for them answers 400.
  • Responses never include output files. To get charts and other files back, use the run endpoints.
  • There is no separate compile stage: compiler errors arrive in run.stderr with a non-zero run.code.
  • run.output is stdout followed by stderr, not interleaved. wall_time is in milliseconds and memory in bytes; cpu_time is null.
  • run.status is RE for a non-zero exit or too many processes, TO for the 30-second timeout, SG for a signal, OL for an output flood and XX for an internal error.

Calling from a web page

Your API key has to stay secret, so a page doesn't call the API itself, and the API sends no CORS headers, so a page on your site can't read its answers anyway. Instead the page posts the same request to a short route on your own server, which adds the key. On Vercel, or any host that runs Next.js, that is one file:

app/api/run/route.ts
// app/api/run/route.ts: the page posts here, and the key stays on the server.
export async function POST(request: Request) {
  // Browsers send the page's Origin with a POST, and pages can't change it.
  if (request.headers.get('origin') !== process.env.SITE_ORIGIN) {
    return new Response('Forbidden', { status: 403 });
  }
  const res = await fetch('https://api.onlinecompiler.io/api/v2/piston/execute', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: process.env.ONLINECOMPILER_API_KEY!,
    },
    body: await request.text(),
  });
  return new Response(res.body, {
    status: res.status,
    headers: { 'Content-Type': 'application/json' },
  });
}

On Netlify the same code goes in netlify/functions/run.mts as export default async (request: Request) => { ... }, with export const config = { path: '/api/run' }. For a static site hosted elsewhere, such as GitHub Pages, a Cloudflare Worker does the same and answers the browser's CORS check:

worker.js
// A Cloudflare Worker. Set SITE_ORIGIN (such as https://example.com) as a
// variable and ONLINECOMPILER_API_KEY as a secret.
export default {
  async fetch(request, env) {
    if (request.headers.get('Origin') !== env.SITE_ORIGIN) {
      return new Response('Forbidden', { status: 403 });
    }
    const cors = {
      'Access-Control-Allow-Origin': env.SITE_ORIGIN,
      'Access-Control-Allow-Methods': 'POST',
      'Access-Control-Allow-Headers': 'Content-Type',
    };
    if (request.method === 'OPTIONS') return new Response(null, { headers: cors });
    if (request.method !== 'POST') return new Response('Method not allowed', { status: 405, headers: cors });
    const res = await fetch('https://api.onlinecompiler.io/api/v2/piston/execute', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', Authorization: env.ONLINECOMPILER_API_KEY },
      body: await request.text(),
    });
    return new Response(res.body, {
      status: res.status,
      headers: { ...cors, 'Content-Type': 'application/json' },
    });
  },
};

The page then sends the request it sent before, to the route:

Page
// In the page: '/api/run' for the Next.js route, or the Worker's URL.
const res = await fetch('/api/run', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ language: 'python', version: '*', files: [{ content: code }], stdin }),
});
const { run } = await res.json(); // run.stdout, run.stderr, run.code

The Origin check keeps other websites from using your route from their visitors' browsers. A script outside a browser can send any Origin, so also give the key a monthly limit on the API keys page, and turn on your host's rate limiting for a busy public page.

Moving from Piston's public API? See Piston API alternatives.

Questions

Do Piston language names work?
Yes. Piston's names and aliases for our 46 languages work, such as py, c++ and csharp. Other languages answer 400 with "runtime is unknown".
Can I pick a language version?
version is accepted and ignored: programs run on the current version that /runtimes lists.
Are command-line arguments supported?
Yes. args reach the program after its command, as on Piston: up to 32 strings and 4,096 bytes in all. sqlite-3, duckdb-1.5 and erlang-29 cannot take them and answer 400.
Is run_memory_limit honoured?
No. It is accepted and ignored, and every run gets our 512 MB: a limit here would count the language's runtime, and a Java, Kotlin or F# program uses over 200 MB just to start. For a lower limit of your own, send memory_limit to the native run-code API.