Skip to content

Code Execution API

Invoke Code Execution

Endpoint to execute Python code through the LLM.

POST https://api.relax.ai/v1/tools/code

Example Request

from relaxai import Relaxai
client = Relaxai(
api_key = RELAX_API_KEY,
)
response = client.tools.execute_code(
code="print(2**2)",
lang="python",
)
print(response)
import Relaxai from 'relaxai';
const client = new Relaxai({
apiKey: 'My API Key',
});
const toolResponse = await client.tools.executeCode({ code: 'code', lang: 'lang' });
console.log(toolResponse.execution_time);
package main
import (
"context"
"fmt"
"github.com/relax-ai/go-sdk"
"github.com/relax-ai/go-sdk/option"
)
func main() {
client := relaxai.NewClient(
option.WithAPIKey("My API Key"),
)
toolResponse, err := client.Tools.ExecuteCode(context.TODO(), relaxai.ToolExecuteCodeParams{
ToolRequest: relaxai.ToolRequestParam{
Code: "code",
Lang: "lang",
},
})
if err != nil {
panic(err.Error())
}
fmt.Printf("%+v
", toolResponse.ExecutionTime)
}
Terminal window
curl https://api.relax.ai/v1/tools/code \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $RELAX_API_KEY" \
-d '{
"code": "print(2**2)",
"lang": "python"
}'

Response

Returns a Chat Completions object.


Response
{
"success": true,
"stdout": "4\n",
"stderr": "",
"exit_code": 0,
"execution_time": 3.788510799407959,
"security_checked": true
}

Request Body

The following parameters can be included in the request body:


Deep Research Request Body

code

  • Type: string
  • Required: Yes
  • Description: Code snippet to execute.

lang

  • Type: string
  • Required: Yes
  • Description: Programming language of the code.

libraries

  • Type: array
  • Required: No
  • Description: Optional list of libraries to include.