# Get the code tree

> Every file and folder in the bucket's code.

`GET https://api.codebuckets.clatterlabs.xyz/v1/buckets/{bucket_id}/tree` (key permission: `buckets:read`)

Lists the code tree as flat paths. Pass `ref` to read a specific version; responses for an explicit `ref` never change, so they're cached at the edge and come back fast.

## Authorizations

- `Authorization` (string, required): Your API key as a bearer token: `Bearer clsk_...`. Create keys in the [Console](https://console.clatterlabs.com/api-keys) with the Code Buckets permissions you need.
- `X-Clatter-Account` (string, required): The Account ID that owns the key, like `CL-7K2M9QXA`. Find it in Console › Settings. A missing value answers `401 missing_account_id`; someone else's answers `401 account_mismatch`.

## Path parameters

- `bucket_id` (string, required): The bucket id (`cbk_…`).

## Query parameters

- `ref` (string): A code ref (40 hex characters) from `bucket.code.ref`, an event, or a backup's `code_ref`. Defaults to the current tree.

## Response

- `object` (string): `tree`.
- `bucket_id` (string): The bucket.
- `ref` (string | null): The ref listed (`null` when there's no code yet).
- `files` (object[]): `{ path, type: "file", size }` and `{ path, type: "dir" }`, sorted by path.
- `truncated` (boolean): `true` for very large trees (over 100,000 entries).

## Example request

```bash
curl "https://api.codebuckets.clatterlabs.xyz/v1/buckets/BUCKET_ID/tree?ref=9b1f0c5e2a7d43e8b6c1f0a9d2e5b7c4a1f3e8d0" \
  -H "Authorization: Bearer $CLATTER_API_KEY" \
  -H "X-Clatter-Account: $CLATTER_ACCOUNT_ID"
```

## Errors

| Status | Code | When |
| --- | --- | --- |
| `401` | `invalid_api_key / missing_account_id / account_mismatch` | The key or Account ID is wrong. |
| `402` | `quota_exceeded` | This month's free tokens are used up. |
| `403` | `insufficient_scope` | The key lacks the permission. |
| `404` | `not_found` | No bucket with that id on your account. |
| `400` | `bad_ref` | `ref` isn't 40 hex characters. |

## Example response

```json
{
  "object": "tree",
  "bucket_id": "cbk_q7c1r9t2y6u0i3o8p5a4",
  "ref": "9b1f0c5e2a7d43e8b6c1f0a9d2e5b7c4a1f3e8d0",
  "files": [
    {
      "path": "README.md",
      "type": "file",
      "size": 1840
    },
    {
      "path": "src",
      "type": "dir"
    },
    {
      "path": "src/index.ts",
      "type": "file",
      "size": 5121
    }
  ],
  "truncated": false
}
```


