This is the HackerEarth API V4 resource documentation. If you face any issues or have any requests, please contact support.
The API provides endpoints for compiling and running code in several languages. It can be accessed via an API key-based authorization process. You must register your web application in order to receive a client-specific client-secret that must be provided while communicating with the API.
Currently, there is a cap on the following:
Register your own client here. Register here for an API key.
V4 will only support asynchronous code evaluation requests, unlike v3 which supported both synchronous and asynchronous. It also fixes issues in the async version of the v3 endpoints. In the asynchronous flow, a temporary response is returned immediately after the code execution request has been received. Internally, the request will be queued for further processing. In the meanwhile, you can use the getStatus API to retrieve the execution status of the code that you just submitted. We also have a callback feature, wherein if you pass a callback URL in the request, we send the response of compilation and execution to this URL. This way the client program doesn't wait on a response from hackerearth. More details are shared in the rest of the document.
In v3 you needed to make 2 API calls to 2 different endpoints to evaluate a piece of code against the given input. In v4 you only need to make 1 API call to the new endpoint, which will take care of compilation as well as the execution of the code.
Instead of sending the authentication details (client-secret) in the payload, you can now send it in the header, which is the preferred method. We have also added 1 reconciliation endpoint, which can be called to get the status of any previous code- evaluation request. The response body will contain the compile and execution results of the code-evaluation request in question.
https://api.hackerearth.com/v4/partner/code-evaluation/submissions/
The client authentication is done using your unique client-secret. You need to register your web application in order to get a unique client-secret, which must be provided while communicating with the API. You need to pass this client secret in the header with the key as client-secret and value as the client secret you receive after registration.
Content type
Request Headers You have to pass 2 mandatory headers:
{
"lang": "PYTHON",
"source": "print 'Hello World'",
"input": "",
"memory_limit": 243232,
"time_limit": 5,
"context": “{‘id’: 213121}”,
"callback": "https://client.com/callback/"
}
{
"request_status": {
"message": "Compilation step is over",
"code": "CODE_COMPILED"
},
"errors": {},
"he_id": "1466ca3e-a263-437a-a996-a816af0db499",
"status_update_url": "https://api.hackerearth.com/v4/partner/code-evaluation/submissions/1466ca3e-a263-437a-a996-a816af0db499",
"context": "{id: 213121}",
"result": {
"run_status": {},
"compile_status": "OK"
}
}
{
"request_status": {
"message": "Your request has been completed successfully",
"code": "REQUEST_COMPLETED"
},
"errors": {},
"he_id": "1466ca3e-a263-437a-a996-a816af0db499",
"status_update_url": "https://api.hackerearth.com/v4/partner/code-evaluation/submissions/1466ca3e-a263-437a-a996-a816af0db499",
"context": "{‘id’: 213121}",
"result": {
"run_status": {
"memory_used": "3392",
"status": "AC",
"time_used": "0.033403",
"signal": "OTHER",
"exit_code": "0",
"status_detail": "NA",
"stderr": "",
"output": "https://he-s3.s3.amazonaws.com/media/userdata/AnonymousUser/code/ee2123423155",
"request_NOT_OK_reason": "",
"request_OK": "True"
},
"compile_status": "OK"
}
}
{
"request_status": {
"message": "Your request has been queued in the evaluation pipeline",
"code": "REQUEST_QUEUED"
},
"he_id": "6438b9a6-d5c0-4960-9049-b21a35f0bdb0",
"result": {
"run_status": {
"status": "NA"
},
"compile_status": "Compiling..."
},
"context": "{‘id’: 213121}",
"status_update_url": "https://api.hackerearth.com/v4/partner/code-evaluation/submissions/6438b9a6-d5c0-4960-9049-b21a3zz5f0bdb0/"
}
Status | Status detail | Explanation |
AC | NA | Accepted. The code executed successfully. |
MLE | NA | Memory Limit Exceeded. Execution of the compiled code used more memory that the memory limit |
TLE | NA | Time Limit Exceeded. Execution of the compiled code took more time than the passed time limit |
RE | SIGXFSZ SIGSEGV SIGFPE SIGBUS SIGABRT NZEC OTHER |
SIGXFSZ: The output file size exceeded beyond allowed system value SIGSEGV: Invalid memory reference or segmentation fault SIGFPE: Division by 0 SIGBUS: Accessed memory location that has not been physically mapped. The address is valid but we failed to read/write. SIGABRT: Aborting due to a fatal error. NZEC or OTHER: If the user code execution stopped because of any reasons other than the above. |
Error Response In case of an error with the above request, the response contains the details of the error.
{
"message": "ArgumentMissingError: source is needed!",
"errors": {}
}
{
"message": "QuotaExceededError: API usage quota exceeded",
"errors": {
"quota": 73000000,
"current_count": 120189
}
}
{
"message": "UnregisteredClientError: Client does not exist",
"errors": { }
}
https://api.hackerearth.com/v4/partner/code-evaluation/submissions/id/
The id
parameter is the value of the he_id
attribute you receive in the response while submitting the POST request for code evaluation. This he_id attribute uniquely identifies each request made by the client.
Like the POST request above, this requires you to pass client-secret in the header for the purpose of authentication. The response format is the same as above.
Python
import json
import requests
CODE_EVALUATION_URL = u'https://api.hackerearth.com/v4/partner/code-evaluation/submissions/'
CLIENT_SECRET = 'bc40b60349b9ddd98cf2fda31c9d1s7112chdsd85hs231'
def execute(source_file_name, language):
source = open(source_file_name, "r")
input_file = open("input.txt", "r")
callback = "https://client.com/callback/"
data = {
'source': source.read(),
'lang': language,
'time_limit': 5,
'memory_limit': 246323,
'input': input_file.read(),
'callback' : callback,
'id': "client-001"
}
headers = {"client-secret": CLIENT_SECRET}
input_file.close()
source.close()
resp = requests.post(CODE_EVALUATION_URL, json=data, headers=headers)
"""
This will also work:
resp = requests.post(CODE_EVALUATION_URL, data=data, headers=headers)
"""
dict = json.loads(resp.text)
return dict
run("py_source.py", "PYTHON")
CURL
curl -X POST \
https://api.hackerearth.com/v4/partner/code-evaluation/submissions/ \
-H 'cache-control: no-cache' \
-H 'client-secret: bc40b60349b9ddd9s8cf2fda31c9d17112cfsdsd54d81' \
-H 'content-type: application/json' \
-d '{"lang": "PYTHON", "memory_limit": 2463232, "callback": "https://client.com/execute/result/", "time_limit": 5,
"source": "print '\''Hello World'\''", "input": "", "id": 21312112}'
Python
import json
import requests
GET_STATUS_URL = u'https://api.hackerearth.com/v4/partner/code-evaluation/submissions/6438b9a6-d5c0-4960-9049-b21a35f0bsdb0/'
CLIENT_SECRET = 'bc40b60349b9ddd98cf2fda31c9d1ds7112chd8s5hs231'
def get_status():
headers = {"client-secret": CLIENT_SECRET}
resp = requests.get(GET_STATUS_URL, headers=headers)
dict = json.loads(resp.text)
return dict
get_status()
CURL
curl -X GET \
https://api.hackerearth.com/v4/partner/code-evaluation/submissions/202fsdsf443-6308-4707-ab35-5c93f5ab8ea6/ \
-H 'client-secret: bc40b60349b9ddd98cf2fda31c9d17112csdsf54d81'
Language | Lang Argument |
C | C |
C++14 | CPP14 |
C++17 | CPP17 |
Clojure | CLOJURE |
C# | CSHARP |
Go | GO |
Haskell | HASKELL |
Java 8 | JAVA8 |
Java 14 | JAVA14 |
JavaScript(Nodejs) | JAVASCRIPT_NODE |
Kotlin | KOTLIN |
Objective C | OBJECTIVEC |
Pascal | PASCAL |
Perl | PERL |
PHP | PHP |
Python 2 | PYTHON |
Python 3 | PYTHON3 |
Python 3.8 | PYTHON3_8 |
R | R |
Ruby | RUBY |
Rust | RUST |
Scala | SCALA |
Swift | SWIFT |
TypeScript | TYPESCRIPT |