## Download a REST document template

`GET /v1/doc-templates/:id/download`

Downloads the source .docx file of an app template with `moduleId: rest` through Vibecode: the platform adds the Bitrix24 authorization to the request itself.

## Parameters

| Parameter | Type | Required | Description |
|----------|-----|:-----:|---------|
| `id` (path) | number | yes | Template ID. List: [`GET /v1/doc-templates`](/docs/entities/doc-templates/list) |

No request body is sent.

## Examples

### curl — personal key

```bash
curl -H "X-Api-Key: YOUR_API_KEY" \
  "https://vibecode.bitrix24.com/v1/doc-templates/237/download" \
  -o template-237.docx
```

### curl — OAuth application

```bash
curl -H "X-Api-Key: YOUR_APP_KEY" \
  -H "Authorization: Bearer USER_SESSION_TOKEN" \
  "https://vibecode.bitrix24.com/v1/doc-templates/237/download" \
  -o template-237.docx
```

### JavaScript — personal key

```javascript
const res = await fetch(
  'https://vibecode.bitrix24.com/v1/doc-templates/237/download',
  { headers: { 'X-Api-Key': 'YOUR_API_KEY' } }
)

if (!res.ok) {
  const body = await res.json()
  throw new Error(body.error?.code ?? String(res.status))
}

const blob = await res.blob()
```

### JavaScript — OAuth application

```javascript
const res = await fetch(
  'https://vibecode.bitrix24.com/v1/doc-templates/237/download',
  {
    headers: {
      'X-Api-Key': 'YOUR_APP_KEY',
      'Authorization': 'Bearer USER_SESSION_TOKEN',
    },
  }
)

if (!res.ok) {
  const body = await res.json()
  throw new Error(body.error?.code ?? String(res.status))
}

const blob = await res.blob()
```

## Response fields

On success, the response has HTTP status `200 OK`, and the body contains the bytes of the .docx file with no JSON envelope. File details come in headers.

| Header | Example value | Description |
|-----------|-----------------|----------|
| `Content-Type` | `application/vnd.openxmlformats-officedocument.wordprocessingml.document` | Content type that Bitrix24 returned for the template file |
| `Content-Disposition` | `attachment; filename="template-237.docx"` | File name for saving. Built from the template `id` as `template-<id>.docx`; the template name is not part of it |

## Response example

```http
HTTP/1.1 200 OK
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Disposition: attachment; filename="template-237.docx"
Transfer-Encoding: chunked

<binary .docx content, 922 bytes, starts with PK>
```

## Error response example

400 — invalid `id`:

```json
{
  "success": false,
  "error": {
    "code": "INVALID_PARAMS",
    "message": "Template ID must be a positive integer."
  }
}
```

## Errors

| HTTP | Code | Description |
|------|-----|----------|
| 400 | `INVALID_PARAMS` | `id` is not a positive integer. Message: `Template ID must be a positive integer.` |
| 404 | `ENTITY_NOT_FOUND` | No template with this `id` exists |
| 404 | `ENTITY_NOT_FOUND` | The template exists, but it is not a `rest` module template or has no download URL. Message: `REST template download not found.` |
| 403 | `BITRIX_ACCESS_DENIED` | The template belongs to another module, for example CRM. CRM templates are downloaded through [`GET /v1/crm-document-templates/:id/download`](/docs/entities/crm-document-templates/download) |
| 502 | `DOWNLOAD_FAILED` | Bitrix24 did not return the file: it responded with an error code, for example `Bitrix24 returned 403`, or returned a URL outside the portal or without HTTPS. An on-premise Bitrix24 portal reachable only over HTTP always gets this error — set up HTTPS on it to transfer files |
| 422 | `BITRIX_ERROR` | Bitrix24 rejected the request to read the template. The reason is in `error.message` |
| 403 | `SCOPE_DENIED` | The key lacks the `documentgenerator` scope |
| 401 | `TOKEN_MISSING` | The key has no configured tokens |
| 401 | `MISSING_API_KEY` | The `X-Api-Key` header is not passed |
| 429 | `RATE_LIMITED` | Bitrix24 rate-limited requests to the portal. Retry after the delay in the `Retry-After` header |

Full list of common API errors — [Errors](/docs/errors).

## Known specifics

**A read-only key works.** Downloading does not count as a write, so a key without write permission gets the file the same way as a full-access key.

**The `downloadMachine` field points here.** In the responses of [`GET /v1/doc-templates/:id`](/docs/entities/doc-templates/get), [`GET /v1/doc-templates`](/docs/entities/doc-templates/list) and [`POST /v1/doc-templates`](/docs/entities/doc-templates/create), this field contains the URL of this endpoint for a template with `moduleId: rest`. When following it, pass the same `X-Api-Key` with the `documentgenerator` scope — the file does not open without a key. For templates of other modules, the field does not contain this endpoint's URL.

## See also

- [Get a template](/docs/entities/doc-templates/get)
- [List templates](/docs/entities/doc-templates/list)
- [Create a template](/docs/entities/doc-templates/create)
- [Document templates](/docs/entities/doc-templates)
- [Download a CRM document template](/docs/entities/crm-document-templates/download)
