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

# Code Contexts

> Create and manage persistent code execution contexts.

## Create a context

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  ctx = machine.code_interpreter.create_code_context(
      cwd="/workspace",
      language="python",
  )
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const ctx = await machine.code.createContext({
    cwd: "/workspace",
    language: "python",
  });
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code context create mch_123 --cwd /workspace --language python
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"cwd": "/workspace", "language": "python"}'
  ```
</CodeGroup>

## Run in a context

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  machine.code_interpreter.run_code("value = 10", context=ctx)
  result = machine.code_interpreter.run_code("value * 2", context=ctx)
  print(result.results[0].text)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  await machine.code.run("value = 10", { context: ctx });
  const result = await machine.code.run("value * 2", { context: ctx });
  console.log(result.results[0].text);
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "value = 10" --context <context-id>
  nullspace machine code run mch_123 "value * 2" --context <context-id>
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # returns a Server-Sent Events stream of JSON events
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "value = 10", "context_id": "<context-id>"}'
  curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
    -H "Content-Type: application/json" \
    -d '{"code": "value * 2", "context_id": "<context-id>"}'
  ```
</CodeGroup>

## List, restart, and remove

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  contexts = machine.code_interpreter.list_code_contexts()
  for item in contexts:
      print(item.id, item.language, item.cwd)

  machine.code_interpreter.restart_code_context(ctx)
  machine.code_interpreter.remove_code_context(ctx)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  const contexts = await machine.code.listContexts();
  for (const item of contexts) {
    console.log(item.id, item.language, item.cwd);
  }

  await machine.code.restartContext(ctx);
  await machine.code.removeContext(ctx);
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code context list mch_123
  nullspace machine code context restart mch_123 <context-id>
  nullspace machine code context remove mch_123 <context-id>
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  curl "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}"
  curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts/<context-id>/restart" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}"
  curl -X DELETE "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts/<context-id>" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}"
  ```
</CodeGroup>

## Interrupt long-running code

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  machine.code_interpreter.interrupt_code_context(ctx)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  await machine.code.interruptContext(ctx);
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code context interrupt mch_123 <context-id>
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts/<context-id>/interrupt" \
    -H "Authorization: Bearer ${NULLSPACE_API_KEY}"
  ```
</CodeGroup>

## Related

* [Runs and artifacts](./runs-artifacts)
* [WebSocket protocol](../reference/websocket-protocol)
