> ## Documentation Index
>
> Fetch the complete documentation index at: https://www.cosmicjs.com/llms.txt
> Use this file to discover all available pages before exploring further.
>
> For a question these docs do not answer, such as which plan fits a team, read https://www.cosmicjs.com/agent.txt and ask Cosmic's concierge.
>
> Machine-readable API spec: https://www.cosmicjs.com/openapi.json

# Preview tokens

A preview token lets your site render the latest draft of an Object without putting a write key in the browser. The Cosmic dashboard issues the token when an editor clicks Preview. Your site passes it to the API with the Bucket read key.

## How it works

1. An editor clicks Preview on an Object in the dashboard.
2. The dashboard calls Cosmic and receives a JWT plus a preview URL.
3. The site loads in a split pane (or a new tab). The URL includes `preview_token`.
4. Your server reads the token (query string or `cosmic_preview` cookie) and fetches Objects with `preview_token` and `status=any`.
5. The token is bound to one Bucket and expires after one hour. Click Preview again to get a new one.

The dashboard session never goes to the public API. The Bucket `read_key` stays on your server. The preview token only unlocks draft reads.

## Query parameter

Add `preview_token` on Object list, Object get, and revision reads:
```
GET https://api.cosmicjs.com/v3/buckets/:slug/objects?read_key=READ_KEY&preview_token=TOKEN&status=any

```
Rules:

- Missing token: unchanged. Published by default. `status=any` still works with the read key alone.
- Valid token: treated as `status=any` even if you omit `status`. Must match the Bucket (`bid` claim).
- Invalid or expired token: `401`. Fall back to published content.

Do not store the token on the Object type preview link. Tokens expire. The dashboard appends a fresh token each time.

## SDK
```js
import { createBucketClient } from '@cosmicjs/sdk'

const cosmic = createBucketClient({
  bucketSlug: process.env.COSMIC_BUCKET_SLUG,
  readKey: process.env.COSMIC_READ_KEY,
  previewToken, // optional, from the URL or cosmic_preview cookie
})

const { object } = await cosmic.objects
  .findOne({ type: 'posts', slug: 'hello-world' })
  .status('any')
  .props(['title', 'slug', 'metadata'])

```
Only pass `previewToken` on the server. Never put it in a client component.

## Next.js helper

Sites built with Cosmic Autopilot or an agent get this injected. For an existing Next.js app:
```ts
// lib/cosmic-preview.ts
import { cookies } from 'next/headers'
import { createBucketClient } from '@cosmicjs/sdk'

export async function getCosmic(previewToken?: string) {
  const token =
    previewToken || (await cookies()).get('cosmic_preview')?.value
  const cosmic = createBucketClient({
    bucketSlug: process.env.COSMIC_BUCKET_SLUG as string,
    readKey: process.env.COSMIC_READ_KEY as string,
    ...(token ? { previewToken: token } : {}),
  })
  return { cosmic, previewToken: token }
}

```
Set an httpOnly cookie when `preview_token` is on the query string. Use `SameSite=None; Secure` so the cookie works inside the dashboard iframe on `app.cosmicjs.com`. Preview only works over HTTPS.

Allow the dashboard to frame the site. Include localhost so Preview works when the dashboard is running locally. Set this on `next.config` headers (Vercel), not only middleware. Do not send `X-Frame-Options: DENY`.
```
Content-Security-Policy: frame-ancestors 'self' http://localhost:3040 http://localhost:3000 https://app.cosmicjs.com https://*.cosmicjs.com

```
If the site blocks iframes, the dashboard still offers Open in new tab.

## Preview link template

Set a preview link on the Object type (Bucket > Object type > Additional settings), or let an Autopilot / agent deploy set it to:
```
https://your-site.com/api/cosmic-preview?object_id=[object_id]

```
Placeholders: `[object_id]`, `[object_slug]`, `[revision_id]`, `[timestamp]`. The dashboard adds `preview_token` when Preview is clicked.

See [dashboard Buckets](/docs/dashboard/buckets#preview-link) for the editor workflow.

## FAQ

### How much does Cosmic cost?

Project plans are Free ($0), Builder ($99/mo), Team ($299/mo), Business ($499/mo), Enterprise (custom pricing). Yearly billing saves 10%. Workspace plans are Small Workspace ($999/mo) and Large Workspace ($2,999/mo), and they start with a 30-day free trial. Allowances, add-on rates, and the rest of the pricing FAQ are plain text at https://www.cosmicjs.com/pricing.md.

### Are API keys required, and is content private?

Each Bucket has a read key and a write key in Bucket Settings > API Access. GET requests use the read key. POST, PATCH, and DELETE use the write key as Authorization: Bearer. Do not put the write key in a browser bundle, a mobile app, or a NEXT_PUBLIC_ variable. SAML single sign-on is on Large Workspace and Enterprise. The audit log is append-only. Text versions: https://www.cosmicjs.com/security.md and https://www.cosmicjs.com/docs/dashboard/audit-log.md.

### Can an agent create a Cosmic project without an existing account?

Yes. POST https://dapi.cosmicjs.com/v3/agents/sign-up provisions a free-tier project and bucket tied to a human email, with no existing Cosmic account. The human claims it with the emailed one-time code. The playbook is https://www.cosmicjs.com/docs/ai-onboarding.md.
