> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gravixlayer.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Run JavaScript

> Execute JavaScript code in runtimes

## Basic Execution

`POST /v1/agents/runtime/{runtime_id}/code/run`

<CodeGroup>
  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from gravixlayer import GravixLayer

  client = GravixLayer()  # defaults to cloud="aws", region="us-east-1"
  sandbox = client.runtime.create()  # defaults to template="base-small"

  result = sandbox.run_code(
      code="console.log('Hello from JavaScript')",
      language="javascript",
  )
  print(result.text)  # Hello from JavaScript

  sandbox.kill()
  ```

  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import { GravixLayer } from 'gravixlayer';

  const client = new GravixLayer(); // defaults to cloud="aws", region="us-east-1"
  const sandbox = await client.runtime.create(); // defaults to template="base-small"

  const result = await sandbox.runCode('console.log(\'Hello from JavaScript\')', { language: 'javascript' });
  console.log(result.text); // Hello from JavaScript

  await sandbox.kill();
  ```

  ```bash CLI theme={"theme":{"light":"github-light","dark":"github-dark"}}
  cat > /tmp/hello.js <<'EOF'
  console.log('Hello from JavaScript')
  EOF

  gravixlayer runtime run "$RT" /tmp/hello.js --timeout 300
  ```

  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -sS -X POST "https://api.gravixlayer.ai/v1/agents/runtime/$RT/code/run" \
    -H "Authorization: Bearer $GRAVIXLAYER_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "code": "console.log('\''Hello from JavaScript'\'')",
      "language": "javascript",
      "timeout": 300
    }' | jq .
  ```
</CodeGroup>

<Info>
  JavaScript execution requires passing `language="javascript"`. The default is `python`. The CLI infers language from the `.js` file extension.
</Info>

## Parameters

| Parameter    | Type    | Required | Description                     |
| ------------ | ------- | -------- | ------------------------------- |
| `code`       | string  | Yes      | JavaScript source code          |
| `language`   | string  | Yes      | Must be `"javascript"`          |
| `context_id` | string  | No       | Reuse a persistent code context |
| `timeout`    | integer | No       | Execution timeout in seconds    |

## Response

| Field     | Type    | Description                                  |
| --------- | ------- | -------------------------------------------- |
| `text`    | string  | Combined stdout output                       |
| `success` | boolean | `True` if no execution error                 |
| `error`   | object  | Error details (`name`, `value`, `traceback`) |
