Skip to content

Repository files navigation

@judge0/judge0-js

License: MIT

The JavaScript SDK for Judge0 (@judge0/judge0-js) — a faithful port of the official judge0-python design and API.

import { run, PYTHON } from '@judge0/judge0-js';

const result = await run({
  source_code: 'print("hello, world")',
  language: PYTHON,
});

console.log(result.stdout);

Installation

npm install @judge0/judge0-js

Quick Start

Getting the API Key

Get your API key from RapidAPI or ATD.

Judge0 has two flavors: Judge0 CE and Judge0 Extra CE. When using Rapid or ATD you may need to subscribe to both if you want access to all languages.

Using Your API Key

Option 1: Explicit Client

import { RapidJudge0CE, run } from '@judge0/judge0-js';

const client = new RapidJudge0CE('your-rapid-api-key');

const result = await run({
  client,
  source_code: 'print("hello, world")',
  language: 'PYTHON',
});

console.log(result.stdout);

Available clients:

  • RapidJudge0CE
  • ATDJudge0CE
  • RapidJudge0ExtraCE
  • ATDJudge0ExtraCE
  • Judge0CloudCE (preview endpoint)

Option 2: Implicit Client (Recommended for most cases)

Set one of the following environment variables:

  • JUDGE0_RAPID_API_KEY
  • JUDGE0_ATD_API_KEY

The SDK will automatically create the appropriate client.

import { run, PYTHON } from '@judge0/judge0-js';

const result = await run({
  source_code: 'print("hello, world")',
  language: PYTHON,
});

console.log(result.stdout);

Examples

Hello, world

import { run, PYTHON } from '@judge0/judge0-js';

const result = await run({
  source_code: 'print("hello, world")',
  language: PYTHON,
});

console.log(result.stdout);

Running C

import { run, C } from '@judge0/judge0-js';

const sourceCode = `
#include <stdio.h>

int main() {
    printf("hello, world\\n");
    return 0;
}
`;

const result = await run({
  source_code: sourceCode,
  language: C,
});

console.log(result.stdout);

Running Java

import { run, JAVA } from '@judge0/judge0-js';

const sourceCode = `
public class Main {
    public static void main(String[] args) {
        System.out.println("hello, world");
    }
}
`;

const result = await run({
  source_code: sourceCode,
  language: JAVA,
});

console.log(result.stdout);

Reading From Standard Input

import { run, C } from '@judge0/judge0-js';

const sourceCode = `
#include <stdio.h>

int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d\\n", a + b);

    char name[10];
    scanf("%s", name);
    printf("Hello, %s!\\n", name);

    return 0;
}
`;

const stdin = `
3 5
Bob
`;

const result = await run({
  source_code: sourceCode,
  stdin,
  language: C,
});

console.log(result.stdout);

Test Cases

import { run, PYTHON } from '@judge0/judge0-js';

const results = await run({
  source_code: "print(f'Hello, {input()}!')",
  language: PYTHON,
  test_cases: [
    ["Bob", "Hello, Bob!"],                    // tuple-style (array)
    { input: "Alice", expected_output: "Hello, Alice!" },
    ["Charlie", "Hello, Charlie!"],
  ],
});

for (const [i, result] of results.entries()) {
  console.log(`--- Test Case #${i + 1} ---`);
  console.log(result.stdout);
  console.log(result.status);
}

Test Cases With Multiple Submissions

import { run, Submission, PYTHON, C } from '@judge0/judge0-js';

const submissions = [
  new Submission({
    source_code: "print(f'Hello, {input()}!')",
    language: PYTHON,
  }),
  new Submission({
    source_code: `
#include <stdio.h>

int main() {
    char name[10];
    scanf("%s", name);
    printf("Hello, %s!\\n", name);
    return 0;
}
    `,
    language: C,
  }),
];

const testCases = [
  ["Bob", "Hello, Bob!"],
  ["Alice", "Hello, Alice!"],
  ["Charlie", "Hello, Charlie!"],
];

const results = await run({
  submissions,
  test_cases: testCases,
});

for (let i = 0; i < submissions.length; i++) {
  console.log(`--- Submission #${i + 1} ---`);
  for (let j = 0; j < testCases.length; j++) {
    const result = results[i * testCases.length + j];
    console.log(`--- Test Case #${j + 1} ---`);
    console.log(result.stdout);
    console.log(result.status);
  }
}

Asynchronous Execution

import { asyncRun, wait, PYTHON } from '@judge0/judge0-js';

const submission = await asyncRun({
  source_code: 'print("hello, world")',
  language: PYTHON,
});

console.log(submission.stdout); // undefined / null (not finished yet)

await wait({ submissions: submission });

console.log(submission.stdout); // "hello, world\n"

Get Languages

import { getClient } from '@judge0/judge0-js';

const client = getClient();
const languages = await client.getLanguages();
console.log(languages);

Language Aliases

The SDK exports the same convenient aliases as the Python SDK:

PYTHON, PYTHON3, PYTHON_FOR_ML, C, CPP, JAVA, JAVASCRIPT,
BASH, GO, RUST, RUBY, PHP, CSHARP, KOTLIN, SWIFT, ...

You can use either the constants or language IDs / names.

Multi-file / Additional Files

import { File, run, PYTHON } from '@judge0/judge0-js';

const main = new File('main.py', 'print("from additional file")');

const result = await run({
  source_code: 'import main',
  additional_files: [main],
  language: PYTHON,
});

Browser / Vanilla Usage

A complete standalone example (no bundler required) is available in the repository:

# Start a local server (from repo root)
python3 -m http.server 8080

# Then open:
# http://localhost:8080/examples/vanilla.html

The example at examples/vanilla.html demonstrates real usage of the built SDK in the browser (with the necessary shims).

Environment Variables

Variable Client
JUDGE0_RAPID_API_KEY RapidJudge0CE / Extra
JUDGE0_ATD_API_KEY ATDJudge0CE / Extra

If no keys are provided, the SDK falls back to Judge0's public preview endpoints (suitable for testing and development).

Development

npm install
npm run build
npm test

License

MIT — same license as the original Judge0 Python SDK.

About

The official JavaScript SDK for Judge0.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages