# Make a link that opens a private preview, for a limited time.

> The answer carries the link's `url` once; Koidog keeps only a hash of its token, and an idempotent replay answers `409 idempotency_secret_not_replayable` instead of showing it again.
> https://koi.dog/docs/api/previews/create-preview-share-link/

`POST /api/v1/previews/{id}/share-links` · Scope: `crews`

Opening the link sends the browser on to the preview's own address with Boat's access token. The link stops working when it expires (`expiresInMinutes`, 5 to 10080, default 60), is revoked, or its preview stops; a browser that already opened it keeps Boat's access until the preview stops, so stop the preview to shut everyone out. A link lets anyone who holds it in, so it is `active` at once only for the session and a key with `approve`; a key with `crews` alone gets a `pending_approval` link and an approval of kind `preview_link` that the founder, or a key with `approve`, decides. The timeline records the key ID, action and outcome.

## Path parameters.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | `string` | yes | The preview's id. |

## Headers.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | `string` | no | Retry the same request with the same key. [Retry rules](https://koi.dog/docs/api/#retries). |

## Body.

An omitted body is read as `{}`.

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `expiresInMinutes` | `5–10080` | no | Minutes until the link stops working, 5 to 10080 (7 days). Left out, 60. It also stops when its preview stops. Default `60`. |
| `label` | `string` | no | A name for the link, shown in its list and on the timeline, such as who it was sent to. 1 to 80 characters. |

## Response.

201 Created · JSON object

- **409 preview_ended**: Refused with `409 preview_ended` for a preview that stopped or failed.
- **422 validation_failed**: Each schema error appears in `details`.

## Request.

```sh
curl -X POST "https://api.koi.dog/api/v1/previews/$ID/share-links" \
    -H "Authorization: Bearer $KOIDOG_API_KEY" \
    -H "Idempotency-Key: $(uuidgen)" \
    -H "Content-Type: application/json" \
    -d '{
      "expiresInMinutes": 120,
      "label": "For the design review"
    }'
```

```js
const res = await fetch(`https://api.koi.dog/api/v1/previews/${process.env.ID}/share-links`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.KOIDOG_API_KEY}`,
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    expiresInMinutes: 120,
    label: "For the design review",
  }),
});
console.log(await res.json());
```

```python
import os
import uuid

import requests

r = requests.post(
    f"https://api.koi.dog/api/v1/previews/{os.environ['ID']}/share-links",
    headers={
        "Authorization": f"Bearer {os.environ['KOIDOG_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
        "expiresInMinutes": 120,
        "label": "For the design review",
    },
)
r.raise_for_status()
print(r.json())
```

## Response example.

```json
{
  "link": {
    "id": "string",
    "previewId": "string",
    "crewId": "string",
    "label": "string",
    "prefix": "string",
    "state": "pending_approval",
    …
  },
  "url": "string"
}
```
