---
title: Open a code workspace
description: Check out a repository to read its code.
url: https://www.hacktron.ai/docs/api/code-workspaces/open-code-workspace
---

# Open a code workspace

Opens a read-only code workspace: a checkout of one connected repository at one commit, which [`POST /code-workspaces/{workspace_id}/query`](/docs/api/code-workspaces/query-code-workspace) searches and reads. Send one of:

- `repository_id`, from [`GET /repos`](/docs/api/repos/list-repos), with an optional `ref`: a branch, tag, or commit. Without `ref`, the workspace opens at the default branch.
- `pull_request_url`, to open a pull or merge request's head commit. The workspace can then diff the pull request against its base, `base_commit_sha`.

The repository must be enabled for Hacktron and hosted on GitHub, GitLab, or Azure DevOps. You need the member role or above.

Everyone in the organisation who opens the same commit shares one workspace with the same `workspace_id`. The first open of a commit can take a minute or two, with `state` set to `preparing`, and queries wait for it to finish. A workspace closes after 30 minutes without a query, and an organisation can have 25 open at once. When a query says the workspace has closed, open it again.

## Request

`POST /code-workspaces`

Send an [API key](/docs/api/authentication) with the `read` scope in the `X-Api-Key` header.

:::examples

```bash title="Request" wrap=false
curl -X POST "https://api.hacktron.ai/v1/code-workspaces" \
  -H "X-Api-Key: $HACKTRON_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "pull_request_url": "https://github.com/acme/api/pull/42"
}'
```

```json title="Response 200 OK" showLineNumbers=false wrap=false
{
  "workspace_id": "123e4567-e89b-12d3-a456-426614174000-4b825dc642cb6eb9a060e54bf8d69288fbee4904-9c1185a5c5e9fc54612808977ee8f548b2258d31",
  "repository_id": "123e4567-e89b-12d3-a456-426614174000",
  "repository": "acme/api",
  "commit_sha": "4b825dc642cb6eb9a060e54bf8d69288fbee4904",
  "base_commit_sha": "9c1185a5c5e9fc54612808977ee8f548b2258d31",
  "pull_request_number": 42,
  "state": "preparing",
  "error": null
}
```

:::

### Body

A required `application/json` body.

| Field | Type | Description |
| --- | --- | --- |
| `repository_id` | string (uuid) | Id from the repository list. Give this, or pull_request_url. |
| `pull_request_url` | string | A pull or merge request's web link: the workspace opens at its head commit and can diff against its base. |
| `ref` | string | Branch, tag or commit to open with repository_id; the default branch when left out. |

## Responses

### 200 OK

| Field | Type | Description |
| --- | --- | --- |
| `workspace_id` | string, required | Pass to the code query endpoint. |
| `repository_id` | string (uuid), required | |
| `repository` | string, required | |
| `commit_sha` | string, required | The commit the workspace reads. |
| `base_commit_sha` | string, required, nullable | The pull request's base commit, which diff compares against; null outside a pull request. |
| `pull_request_number` | number, required, nullable | |
| `state` | string, required | preparing while the code is checked out; queries wait for it. One of `preparing`, `ready`, `failed`, or `closed`. |
| `error` | string, required, nullable | Why the workspace failed. |

### 400 Bad Request

The repository isn't enabled for Hacktron, or its provider has no code workspaces.

### 404 Not Found

The repository or pull request wasn't found.

### 429 Too Many Requests

The organisation has 25 workspaces open, the most at once, or this one is busy with other queries.

### 503 Service Unavailable

The repository's provider could not be read, or code workspaces aren't responding; try again later.
