> For the complete Lynkow documentation index in agent-friendly format, see [llms.txt](/llms.txt).

# Build a Site with Astro

**Published**: 2026-11-30
**Category**: Getting Started

> Build a Lynkow site on Astro: generate the starter, or add `@lynkow/astro` to an existing project and wire the middleware, the cached clients, the revalidation webhook, the SEO head, the buyer session and the visual editor. Previous: [Guide 26](/sdk/advanced/server-key-buyer-ip) | Next: [Guide 12](/sdk/advanced/error-handling)

> **Reference** - This guide is a tutorial. For exhaustive, always-current signatures and field lists, see the auto-generated reference:
> 
> - SDK: [SDK Reference](/sdk), [SDK Types](/sdk/types)
> - API: [Storefront API Overview](/storefront-api/overview)

## Prerequisites

- A Lynkow site and its ID (a UUID).
- Node.js 22.23.4 or later.
- For your own project: Astro 7 with `output: 'server'` and the `@astrojs/node` adapter. Every page renders on request, so an edit in the Lynkow dashboard is live without a rebuild.

## 1. Generate the starter

```bash
npx create-lynkow-app my-site --template astro --site-id <your-site-id>
```

The starter has the same pages, features and site blocks as the Next.js one: home, blog, shop, cart, checkout, customer accounts, bookings, search, contact form, reviews, `sitemap.xml`, `robots.txt` and `llms.txt`. The code is yours to change. Without `--template`, a terminal asks which starter to generate.

Start it with `npm run dev`, then open `http://localhost:3000`. [Quick Start](/sdk/getting-started/quick-start) lists the other flags.

The rest of this guide shows what the starter contains, so you can add the same pieces to a project of your own.

## 2. Install and configure

```bash
npm install @lynkow/astro lynkow
```

`@lynkow/astro` requires `lynkow` 1.65.6 or a later 1.65.x. It reads no environment variable on its own: your code reads them and passes the values to `resolveConfig`, which throws one error listing every problem.

```ts
// src/lib/config.ts
import { resolveConfig } from '@lynkow/astro'

export const config = resolveConfig(
  {
    siteId: import.meta.env.PUBLIC_LYNKOW_SITE_ID,
    siteUrl: import.meta.env.PUBLIC_SITE_URL,
    locales: import.meta.env.PUBLIC_LOCALES,
    defaultLocale: import.meta.env.PUBLIC_DEFAULT_LOCALE,
    apiUrl: import.meta.env.PUBLIC_LYNKOW_API_URL,
    publishableKey: import.meta.env.PUBLIC_LYNKOW_PUBLISHABLE_KEY,
  },
  { siteId: 'PUBLIC_LYNKOW_SITE_ID', siteUrl: 'PUBLIC_SITE_URL' }
)
```

Register the integration in `astro.config.mjs`:

```js
// astro.config.mjs
import { defineConfig } from 'astro/config'
import node from '@astrojs/node'
import lynkow from '@lynkow/astro/integration'

export default defineConfig({
  output: 'server',
  adapter: node({ mode: 'standalone' }),
  integrations: [lynkow()],
})
```

In `astro dev`, the integration copies the server-only variables of your `.env` files into `process.env`. In production, start the server with them in its environment.

## 3. Create the clients and the data cache

Without `locales`, the site's languages are read from Lynkow at runtime: a language enabled in the dashboard reaches the site without a rebuild.

```ts
// src/lib/lynkow.ts
import { createClients, createLocalePaths, createSiteLocales } from '@lynkow/astro'
import { config } from './config'

export const siteLocales = createSiteLocales({ client: (locale) => lynkow(locale) })
export const lynkow = createClients(config, { siteLocales })
export const paths = createLocalePaths(config, siteLocales)
```

Astro has no data cache for `fetch`, so the adapter brings one. A read is cached when its options carry `cached(tags, revalidate)`:

```ts
import { cached, REVALIDATE, TAGS } from '@lynkow/astro'

const page = await lynkow(locale).pages.getBySlug('home', cached([TAGS.page('home')], REVALIDATE.page))
```

- `revalidate` is the ceiling in seconds. An older entry starts a background load of a fresh copy, and every read gets that stale entry until the fresh copy lands.
- The tags are what the revalidation webhook evicts, so an edit is live on the next request.
- A read without `cached()`, or with `uncached()`, goes to the API every time.

Each server instance keeps its own cache, and the revalidation webhook of section 5 refreshes it.

## 4. Add the middleware

```ts
// src/middleware.ts
import { sequence } from 'astro:middleware'
import { createLynkowMiddleware } from '@lynkow/astro/middleware'
import { config } from './lib/config'
import { lynkow, siteLocales } from './lib/lynkow'

export const onRequest = sequence(
  createLynkowMiddleware({
    config,
    client: (locale, options) => lynkow(locale, options),
    siteLocales,
  })
)
```

On every page request it applies the redirects stored in Lynkow, detects the visitor's language on a first visit to `/`, and routes the locale. The default locale has no prefix: `/about` renders `src/pages/[locale]/about.astro` with `locale` set to the default. Other locales live under their prefix, such as `/fr/about`. It also sets the framing rule the visual editor needs (section 8) and fills the request store that the buyer session reads (section 7).

## 5. Revalidate on content changes

```ts
// src/pages/api/revalidate.ts
import { createRevalidateEndpoint } from '@lynkow/astro/server'
import { siteLocales } from '@/lib/lynkow'

export const prerender = false
export const POST = createRevalidateEndpoint({
  secret: process.env.LYNKOW_WEBHOOK_SECRET,
  siteLocales,
})
```

Create a webhook in the dashboard that points at `https://example.com/api/revalidate`, with the same secret, as described in [Webhooks & Cache Revalidation](/sdk/advanced/webhooks-revalidation). Each signed event evicts the cache tags it can change. The endpoint answers `503` without a secret, `401` for a missing or wrong signature, and `413` for a body over 5 MiB.

## 6. Render the SEO head

`createLynkowServer` builds the head data of each kind of page. It returns flat objects, and your layout renders them with plain tags.

```ts
// src/lib/server.ts
import { cached, REVALIDATE, TAGS } from '@lynkow/astro'
import { createLynkowServer } from '@lynkow/astro/server'
import { config } from './config'
import { lynkow, siteLocales } from './lynkow'

export const server = createLynkowServer({
  config,
  client: (locale) => lynkow(locale),
  getSiteDomain: async (locale) => {
    const response = await lynkow(locale).blocks.siteConfig(cached([TAGS.site], REVALIDATE.site))
    return response.data.site.domain
  },
  siteLocales,
})
```

`pageHead`, `routeHead`, `articleHead`, `categoryHead`, `productHead`, `productCollectionHead` and `productCategoryHead` return a title, a description, the canonical URL, the hreflang set, `robots`, and the Open Graph and Twitter tags. `articleHead` also returns the `article:*` values in `openGraph.article`. Call them after `await siteLocales.get()`.

The starter renders them in `src/components/seo/SeoHead.astro`. Sitemaps, `robots.txt` and `llms.txt` are text routes:

```ts
// src/pages/sitemap.xml.ts
import { XML } from '@lynkow/astro/server'
import { server } from '@/lib/server'

export const prerender = false
export const GET = server.textRoute((client, options) => client.seo.sitemap(options), XML)
```

For structured data, `await server.jsonLd(nodes, locale)` returns the payload of one `<script type="application/ld+json">`, already escaped for `set:html`. See [JSON-LD Structured Data](/sdk/features/json-ld-cascade).

## 7. Add the buyer session

A buyer's cart and account are read through a client created for one request, never through the shared cached clients:

```astro
---
// src/pages/[locale]/cart.astro
import { createBuyerClient } from '@lynkow/astro/server'
import { config } from '@/lib/config'

const buyer = createBuyerClient(config, Astro.cookies, { locale: Astro.params.locale })
const cart = await buyer.cart.retrieve()
---
```

`setCartCookie` and `setCustomerCookie` store the tokens in `__Host-` cookies (`Secure`, `HttpOnly`, `SameSite=Lax`) from an endpoint or an Astro action. `clearBuyerCookies` and `signOut` remove them. Call `assertSameOrigin(request)` before any buyer write that a form can post.

Set `LYNKOW_SERVER_KEY` to the site's server key and every buyer write forwards the buyer's IP address, so the storefront rate limits count each buyer instead of your server. See [Server Key and Buyer IP](/sdk/advanced/server-key-buyer-ip).

## 8. Connect the visual editor

The dashboard opens your site in a frame and edits marked fields in place. Mark each block and field, then start the editor once from the base layout:

```astro
---
// src/components/Hero.astro
const { data } = Astro.props
---
<section data-lynkow-block="hero">
  <h1 data-lynkow-field="title" data-lynkow-type="text" data-lynkow-label="Title">{data.title}</h1>
  <div data-lynkow-field="body" data-lynkow-type="richtext" set:html={data.body} />
</section>
```

```astro
<!-- src/layouts/Base.astro -->
<script>
  import { startVisualEditor } from '@lynkow/astro/client'
  startVisualEditor()
</script>
```

Outside the dashboard's preview frame, `startVisualEditor` does nothing. Inside it, each change is written into the page as it is typed. In a React island, `Editable` and `EditableBlock` from `@lynkow/astro/react` write the same markers, and `useLynkowField` and `useBlockData` follow the editor without a provider. See [Visual Editor](/sdk/advanced/visual-editor) for the field types.

## Environment variables

| Variable | Scope | Purpose |
| --- | --- | --- |
| `PUBLIC_LYNKOW_SITE_ID` | Public | The site's UUID. Required |
| `PUBLIC_SITE_URL` | Public | The origin the site is served from, with no trailing slash. Required |
| `PUBLIC_LOCALES`, `PUBLIC_DEFAULT_LOCALE` | Public | Pin the languages instead of reading them from Lynkow |
| `PUBLIC_LYNKOW_API_URL` | Public | The API origin, for a non-default environment |
| `PUBLIC_LYNKOW_PUBLISHABLE_KEY` | Public | The site's publishable key (`lkw_pk_...`), needed by the store |
| `PUBLIC_LYNKOW_CHECKOUT_MODE` | Public | `embedded` (default) or `hosted`: where the card payment form appears |
| `PUBLIC_LYNKOW_CDN_HOST` | Public | The host Lynkow media resolves to |
| `LYNKOW_WEBHOOK_SECRET` | Server only | Verifies the revalidation webhook |
| `LYNKOW_SERVER_KEY` | Server only | Forwards the buyer's IP on writes |
| `LYNKOW_CLIENT_IP_HEADER` | Server only | The header your CDN writes the visitor's address in |
| `LYNKOW_COUNTRY_HEADERS` | Server only | The header that carries the visitor's country, which picks the store's market |


A `PUBLIC_*` value is inlined in the browser bundle: never put a secret in one. Read each with a literal `import.meta.env.PUBLIC_NAME` so Vite can inline it.

## Deploy

The starter builds a Node server: `npm run build`, then `npm start`, which runs `node --env-file-if-exists=.env.local ./dist/server/entry.mjs` and listens on `$HOST` and `$PORT` when set, else on port 3000. Run it anywhere that runs Node.js, with the server-only variables in its environment, and set `PUBLIC_SITE_URL` to the public origin before you deploy.

The starter adds its security headers through a middleware. The Node server answers the built assets under `/_astro/` and the files of `public/` before any middleware runs, so they carry no such headers. If you need them there, set the same headers at your reverse proxy or CDN. The starter sets no `X-Frame-Options`, because the visual editor frames the site and the middleware varies `frame-ancestors` per request.

## Troubleshooting

| Symptom | Cause | Fix |
| --- | --- | --- |
| An edit does not show up | The webhook does not reach `/api/revalidate`, or its secret differs | Send a test delivery from the webhook's row and check its status code |
| `503` from `/api/revalidate` | `LYNKOW_WEBHOOK_SECRET` is not in the server's environment | Set it and restart the server |
| A language enabled in the dashboard is missing | `PUBLIC_LOCALES` pins the list | Remove it, or add the language to it |
| The editor frame stays empty | A header or CSP of your own forbids framing | Let the Lynkow middleware set `frame-ancestors`, and do not send `X-Frame-Options` |
| Buyers hit rate limits together | The server key is not set | Set `LYNKOW_SERVER_KEY` |


## Next Steps

- [Quick Start](/sdk/getting-started/quick-start)
- [Cart and Checkout](/sdk/features/commerce-cart-checkout)
- [Customer Accounts](/sdk/features/commerce-customer-accounts)