This guide adds Lynkow to a site you already have, with the Next.js App Router. To start a new site instead, follow the Quick Start.

Reference - This guide is a tutorial. For exhaustive, always-current signatures and field lists, see the auto-generated reference:

Prerequisites

  • Node.js 20.9 or later (the SDK alone runs on Node 18)

  • A Lynkow site, and its Site ID: the admin shows it under Settings > Developers > Storefront

Install

Bash
npm install lynkow

If you work with an AI coding assistant, also install the Lynkow AI skills from the project root. Install the Lynkow AI Skills explains the formats.

Bash
npx @lynkow/skills@latest

Create the client

Add your Site ID to .env.local:

env
NEXT_PUBLIC_LYNKOW_SITE_ID=your-site-id

Create one shared client in lib/lynkow.ts and import it everywhere:

TypeScript
import { createClient } from 'lynkow'

export const lynkow = createClient({
  siteId: process.env.NEXT_PUBLIC_LYNKOW_SITE_ID!,
  publishableKey: process.env.NEXT_PUBLIC_LYNKOW_PUBLISHABLE_KEY,
  fetchOptions: { next: { revalidate: 60 } },
})

Option

Default

Use

siteId

required

Your Site ID.

publishableKey

none

The site's storefront key (lkw_pk_...), from Settings > Developers > Storefront. Optional for content, required for commerce.

locale

the site's default locale

Locale of every request. See Multi-language.

fetchOptions

{}

Merged into every fetch call. { next: { revalidate: 60 } } caches responses for 60 seconds.

retry

true

Retries reads on 429, 503 and network errors. false disables it.

The SDK Reference lists the other options. Never set serverKey or clientIp on this shared client: a call made for one buyer from your server needs its own client (Server Key and Buyer IP). To refresh pages as soon as content changes, add a webhook: see Webhooks & Cache Revalidation.

List articles

app/blog/page.tsx:

tsx
import { lynkow } from '@/lib/lynkow'

export default async function BlogPage() {
  const { data: articles, meta } = await lynkow.contents.list({
    limit: 10,
    sort: 'published_at',
    order: 'desc',
  })

  return (
    <main>
      <h1>Blog</h1>
      <ul>
        {articles.map((article) => (
          <li key={article.id}>
            <a href={`/blog/${article.slug}`}>{article.title}</a>
            {article.excerpt && <p>{article.excerpt}</p>}
          </li>
        ))}
      </ul>
      <p>Page {meta.currentPage} of {meta.lastPage}</p>
    </main>
  )
}

List rows carry no body. Read the article itself for it.

Show one article

app/blog/[slug]/page.tsx:

tsx
import { notFound } from 'next/navigation'
import { isLynkowError } from 'lynkow'
import { lynkow } from '@/lib/lynkow'

export default async function ArticlePage({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params

  let article
  try {
    article = await lynkow.contents.getBySlug(slug)
  } catch (error) {
    if (isLynkowError(error) && error.code === 'NOT_FOUND') notFound()
    throw error
  }

  return (
    <article>
      <h1>{article.title}</h1>
      {article.author && <p>By {article.author.fullName}</p>}
      <div dangerouslySetInnerHTML={{ __html: article.body }} />
    </article>
  )
}

body is rendered HTML. The article also carries its categories, tags and SEO fields: see the SDK Types. Error Handling & Resilience covers the other error codes.

Next Steps