Skip to content

thealcodingclub/CodeExecutionAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Execution API

This API is used to execute code snippets in different languages. Given below are the details of this API

Route: /execute

This route takes 4 fields:

  1. langauge: The language of the code snippet.

    Click to see supported languages
    • python
    • rust
    • cpp
    • c
    • java
  2. code: The code snippet to be executed.

  3. timeout: The maximum time in seconds for which the code should run. If the code runs for more than this time, it will be terminated.

    • default: 5 seconds
    • max: 60 seconds
  4. max_memory: The maximum memory in KB (kilobytes) that the code can use. If the code uses more memory than this, it will be terminated.

    • default: 32768KB (or 32MB)
    • max: 131072KB (or 128MB)

Request body format (Example 1):

{
    "language": "python",
    "code": "print('Hello World')"
}
Click to copy curl command
curl --location 'https://codeapi.anga.codes/execute' \
--header 'Content-Type: application/json' \
--data '{
    "language": "python",
    "code": "print('\''Hello World'\'')"
}'

Response body format (Example 1):

{
    "output": "Hello World\n",  // Output of the code
    "error": "",                // If any error occurs during execution
    "memory_used": "13808 KB",  // RAM used (in KB)
    "cpu_time": "125.034027ms"  // in Seconds
}

Request body format (Example 2):

{
    "language": "python",
    "code": "import time\nprint('Hello World')\ntime.sleep(5)",
    "timeout": 2,       // in seconds (defaults to 5, max 60)
}
Click to copy curl command
curl --location 'https://codeapi.anga.codes/execute' \
--header 'Content-Type: application/json' \
--data '{
    "language": "python",
    "code": "import time\nprint('\''Hello World'\'')\ntime.sleep(5)",
    "timeout": 2       
}'

Response body format (Example 2):

{
    "output": "",                   // No output is returned on timeout
    "error": "Execution Timed Out", // Error message in case of timeout
    "memory_used": "15856 KB",      // RAM used (in KB)
    "cpu_time": "2.000637132s"      // Time before code was terminated
}

Request body format (Example 3):

{
    "language": "python",
    "code": "import random;[random.random() for x in range(10**7)]",
    "max_memory": 300000        // in KB (defaults to 32768, max 131072)
}
Click to copy curl command
curl --location 'https://codeapi.anga.codes/execute' \
--header 'Content-Type: application/json' \
--data '{
    "language": "python",
    "code": "import random;[random.random() for x in range(10**7)]",
    "max_memory": 300000
}'

Response body format (Example 3):

{
    "output": "",
    "error": "Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\n  File \"<string>\", line 1, in <listcomp>\nMemoryError\n",
    "memory_used": "283964 KB",
    "cpu_time": "803.820445ms"
}

Request body format (Example 4):

{
    "language": "python",
    "code": "a = input()\nprint(f'first value entered is {a}.')\nb=input()\nprint(f'second value entered is {b}.')",
    "inputs": [
        "bob",
        "alice"
    ]
}
Click to copy curl command
curl --location 'https://codeapi.anga.codes/execute' \
--header 'Content-Type: application/json' \
--data '{
    "language": "python",
    "code": "a = input()\nprint(f'\''first value entered is {a}.'\'')\nb=input()\nprint(f'\''second value entered is {b}.'\'')",
    "inputs": [
        "bob",
        "alice"
    ]
}'

Response body format (Example 4):

{
    "output": "first value entered is bob.\nsecond value entered is alice.\n",
    "error": "",
    "memory_used": "13400 KB",
    "cpu_time": "135.266682ms"
}

About

An API to execute code snippets in multiple languages securely

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages