SDKs overview

Judge0 CE provides client SDKs and libraries designed to facilitate interaction with its code execution and grading API. These tools abstract the underlying HTTP requests, allowing developers to integrate Judge0 CE functionalities into their applications using native language constructs. The SDKs cover a range of popular programming languages, offering methods for submitting code, retrieving submission statuses, and managing compiler/interpreter environments. Both officially supported SDKs and community-contributed libraries are available to accommodate diverse development environments and project requirements. The primary objective of these SDKs is to reduce the boilerplate code required for API communication, enabling developers to focus on application logic rather than low-level request handling. This approach aligns with common API integration patterns observed in many developer tools, where language-specific clients enhance productivity and reduce implementation complexity Twilio's API libraries overview.

The SDKs typically handle aspects such as API key management, request serialization (e.g., converting data structures to JSON), response deserialization, and error handling. This standardization helps ensure consistent and reliable interaction with the Judge0 CE API across different programming environments. For self-hosted instances of Judge0 CE, these SDKs connect to the local API endpoint, providing the same programmatic interface as the cloud-hosted version, which is detailed in the Judge0 documentation.

Official SDKs by language

Judge0 CE offers official SDKs for several programming languages, developed and maintained to provide reliable and up-to-date interfaces for the API. These SDKs are generally recommended for new projects due to their direct support from the Judge0 team and consistent adherence to the API's specifications. Each SDK focuses on providing idiomatic access to the API's core functionalities, such as submitting code for execution, checking submission status, and managing language configurations.

The following table outlines the officially supported SDKs, including their respective package names and typical installation commands:

Language Package Name / Repository Installation Command (Example) Maturity
Python judge0api pip install judge0api Stable
JavaScript judge0-js (npm) npm install judge0-js Stable
Go github.com/judge0/go-sdk go get github.com/judge0/go-sdk Stable
PHP judge0/php-sdk (Composer) composer require judge0/php-sdk Stable
Ruby judge0-ruby (Gem) gem install judge0-ruby Stable
Java judge0-java-sdk (Maven/Gradle) Add to pom.xml or build.gradle Stable
C# Judge0.DotNet (NuGet) dotnet add package Judge0.DotNet Stable
Rust judge0_sdk (Crates.io) Add to Cargo.toml Stable

For detailed usage instructions and API specific methods, developers should refer to the Judge0 API reference documentation, which provides comprehensive information on each endpoint and its parameters.

Installation

Installing Judge0 CE SDKs typically involves using the package manager specific to the programming language. The process is designed to be straightforward, allowing developers to quickly add the necessary libraries to their projects. Below, we provide general installation guidelines and specific examples for popular languages.

Python

The Python SDK for Judge0 CE is available via PyPI. You can install it using pip:

pip install judge0api

After installation, you can import the library into your Python scripts and begin interacting with the Judge0 API. It is good practice to install packages within a Python virtual environment to manage dependencies effectively.

JavaScript (Node.js/Browser)

For JavaScript environments, including Node.js projects and front-end applications, the SDK is available through npm:

npm install judge0-js

This command adds the judge0-js package to your project's node_modules directory and updates your package.json file. You can then import it using ES modules or CommonJS syntax.

Go

The Go SDK can be fetched using the go get command, which retrieves the package and its dependencies:

go get github.com/judge0/go-sdk

Ensure your Go environment is properly set up, and your project is configured to manage modules. The SDK will be added to your module cache.

PHP

PHP projects typically use Composer for dependency management. To install the Judge0 PHP SDK:

composer require judge0/php-sdk

This command adds the SDK to your composer.json and downloads it into your vendor directory. Remember to include Composer's autoloader in your application.

Ruby

For Ruby applications, the SDK is available as a Gem:

gem install judge0-ruby

Once installed, you can require the gem in your Ruby scripts to access the Judge0 client.

Java

Java developers can integrate the Judge0 SDK using Maven or Gradle. For Maven, add the dependency to your pom.xml:

<dependency>
    <groupId>com.judge0</groupId>
    <artifactId>judge0-java-sdk</artifactId>
    <version>1.0.0</version> <!-- Replace with actual version -->
</dependency>

For Gradle, add it to your build.gradle file:

implementation 'com.judge0:judge0-java-sdk:1.0.0' // Replace with actual version

C#

For .NET projects, the C# SDK is distributed as a NuGet package:

dotnet add package Judge0.DotNet

You can also install it via the NuGet Package Manager in Visual Studio. Ensure you are using the correct target framework for your project.

Rust

Rust projects manage dependencies with Cargo. Add the Judge0 SDK to your Cargo.toml file:

[dependencies]
judge0_sdk = "0.1.0" # Replace with actual version

Then, build your project using cargo build, and the SDK will be fetched and compiled.

Quickstart example

This quickstart example demonstrates how to submit a simple Python code snippet for execution using the Judge0 CE Python SDK and retrieve the result. This process involves initializing the client, defining the submission payload, sending the request, and polling for the execution status.

Python Quickstart

First, ensure you have the judge0api package installed (pip install judge0api).

import time
from judge0api import Judge0API

# Initialize the Judge0 API client
# For Judge0 CE (self-hosted), you might need to specify the host URL
# For example: client = Judge0API(host='http://localhost:2358')
client = Judge0API() # Assumes default host or environment variable configured

def submit_and_get_result(source_code, language_id, stdin=""):
    try:
        # 1. Create a submission
        submission = client.create_submission(
            source_code=source_code,
            language_id=language_id,
            stdin=stdin,
            wait=False, # Do not wait for result, we will poll manually
            # base64_encoded=False # Set to True if source_code is base64 encoded
        )
        token = submission['token']
        print(f"Submission created with token: {token}")

        # 2. Poll for the submission status
        while True:
            submission_result = client.get_submission(token)
            status_id = submission_result['status']['id']
            # Status IDs: 1 (In Queue), 2 (Processing), 3 (Accepted), 4 (Wrong Answer), 5 (Time Limit Exceeded), etc.
            # See https://judge0.com/docs/api/statuses for full list
            
            if status_id == 1 or status_id == 2:
                print("Submission still in queue or processing...")
                time.sleep(2) # Wait for 2 seconds before polling again
            else:
                print("Submission finished.")
                print(f"Status: {submission_result['status']['description']}")
                if submission_result['stdout']:
                    print(f"Stdout: {submission_result['stdout']}")
                if submission_result['stderr']:
                    print(f"Stderr: {submission_result['stderr']}")
                if submission_result['compile_output']:
                    print(f"Compile Output: {submission_result['compile_output']}")
                if submission_result['time']:
                    print(f"Time: {submission_result['time']}s")
                if submission_result['memory']:
                    print(f"Memory: {submission_result['memory']}KB")
                break

    except Exception as e:
        print(f"An error occurred: {e}")

# Example usage:
python_code = """print('Hello, Judge0 CE!');
a = 10
b = 20
print(f'Sum: {a+b}')"""

# Language ID for Python 3 (check Judge0 API documentation for specific IDs)
# You can get a list of supported languages and their IDs via client.get_languages()
python3_language_id = 71 # This ID might vary, consult Judge0 API docs

print("\n--- Executing Python Code ---")
submit_and_get_result(python_code, python3_language_id)

# Example with stdin
cpp_code = """#include <iostream>

int main() {
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b << std::endl;
    return 0;
}"""

# Language ID for C++ (GCC 9.2.0)
cpp_language_id = 54 # This ID might vary, consult Judge0 API docs

print("\n--- Executing C++ Code with Input ---")
submit_and_get_result(cpp_code, cpp_language_id, stdin="5\n7")

This example first defines a function submit_and_get_result that takes source code, a language ID, and optional standard input. It then initializes the Judge0API client. A submission is created, and its token is used to repeatedly poll the API for the execution status until the submission is no longer in a 'processing' state. Finally, it prints the output, error messages, and execution metrics. The example demonstrates both a simple Python script and a C++ script with standard input, showcasing the versatility of the Judge0 CE API. Developers can find current language IDs by invoking the client.get_languages() method or by consulting the Judge0 API documentation.

Community libraries

Beyond the officially supported SDKs, the Judge0 ecosystem benefits from various community-contributed libraries and wrappers. These libraries are developed and maintained by individual developers or groups within the Judge0 community. They often extend functionality, provide alternative interfaces, or offer support for languages not covered by official SDKs. While community libraries can provide valuable tools, their maintenance, feature set, and compatibility with the latest API versions may vary. It is recommended to review their documentation, community activity, and licensing before integrating them into production environments.

Some community efforts may include:

  • Framework-specific integrations: Libraries tailored for web frameworks like Django, Flask, Ruby on Rails, or Laravel, simplifying the integration of Judge0 CE within these ecosystems.
  • UI components: Front-end libraries or components that abstract the API calls and provide ready-to-use user interfaces for code submission and result display.
  • Language wrappers: Unofficial client libraries for languages not yet covered by the official SDKs, allowing a broader range of developers to interact with Judge0 CE.
  • Utility tools: Scripts or command-line tools that leverage the Judge0 API for specific tasks, such as bulk testing or automated setup.

Developers looking for community libraries are encouraged to explore platforms like GitHub, npm, PyPI, and other language-specific package repositories. Searching for "Judge0" or "Judge0 API" often yields relevant community projects. Always verify the project's activity, issue tracker, and last update date to gauge its ongoing support and compatibility with the Judge0 CE documentation.