Make the storefront rate limits count each buyer when your own server calls the API for them: the server key, where the buyer IP comes from, how to check it works, and what to do when it does not. Previous: Guide 25 | Next: Guide 27
Reference - This guide is a tutorial. For exhaustive, always-current signatures and field lists, see the auto-generated reference:
Prerequisites
A site that sells or takes bookings, with its publishable key on the client (Guide 25).
Access to Settings > Developers > Storefront in the Lynkow admin.
Do you need it
The API limits each client: one person typing a wrong password, one buyer filling a cart. When your server calls the API for a buyer, the API sees your server's address, the same for every buyer. Without a server key, all your buyers share one counter. Ten mistyped passwords from one person then block sign-in for every buyer for 15 minutes, and ten orders in 15 minutes block checkout for everyone.
Your storefront calls the API from | Needed |
|---|---|
The buyer's browser (the SDK in a client component or page script) | No. The API already sees the buyer's own address. |
Your Next.js server for a buyer (Server Actions, Route Handlers, Server Components) | Yes |
Another server stack (PHP, Python, Go, the backend of a mobile app) | Yes, with two request headers |
Your server, for content every visitor sees (pages, posts, products) | No |
Limits counted per buyer
Each limit is counted per site and per client. The client is the buyer's IP when your server forwards it, and your server's address when it does not.
Calls | Limit | What counts |
|---|---|---|
Cart changes: add, update or remove a line, set the cart's language, address or shipping option, claim a guest cart | 120 per minute | Every request |
Checkout, bookings, saved address changes, the guest order page, form and review submissions | 10 per 15 minutes | Every request |
Sign-in, account activation, email verification, password reset confirmation | 10 failed attempts per 15 minutes, shared by the four | Failed attempts only |
Sign-in for one email address | 5 failed attempts per 15 minutes | Failed attempts only, from any client |
Sign-up, password reset requests, verification email resends | 5 per 15 minutes | Every request |
A refused call returns 429 RATE_LIMIT_EXCEEDED, which the SDK throws as 'TOO_MANY_REQUESTS'. The failed-attempt limits also send a Retry-After header. Once a client reaches its 10 failed attempts, it is paused for 15 minutes, even with the right password. The per-email limit does not depend on the client, so forwarding does not change it.
These figures are the current values, not a contract.
Get a server key
Open Settings > Developers > Storefront and find Server keys, below the storefront keys.
A new site already has one, written into the
.envshown on the last screen of site creation asLYNKOW_SERVER_KEY.Otherwise, or if you no longer have it, click New server key, or Regenerate on the existing one. The key starts with
lkw_srv_and is shown once: copy it into your server's environment.
The key only vouches for the buyer IP your server sends. It reads and changes nothing. Your plan allows as many server keys as storefront keys, counted separately. Keys are managed by roles with the Manage storefront and server keys permission.
A secret API token from Settings > Developers > API also works in place of the server key. Prefer the server key: an API token is an admin credential, so a leak gives access to your site's data.
Set up a site generated with create-lynkow-app
Set
LYNKOW_SERVER_KEYto the server key in your host's environment variables. Never give it aNEXT_PUBLIC_name.Redeploy.
A site generated with create-lynkow-app 0.2.13 or later forwards on its own: it needs nothing else. An older site needs @lynkow/next 0.7.3 and lynkow 1.65.6. When its package.json names older versions, or its src/lib/buyer-forwarding.ts defines a buyerForwarding() function, follow Upgrade an older site first. Set LYNKOW_CLIENT_IP_HEADER only when Choose where the buyer IP comes from says so.
Set up your own Next.js code
With @lynkow/next 0.7.3 and lynkow 1.65.6, createBuyerClient() forwards on its own once LYNKOW_SERVER_KEY is set:
npm install @lynkow/next@^0.7.3 lynkow@~1.65.6// app/actions/cart.ts
'use server'
import { cookies } from 'next/headers'
import { createBuyerClient } from '@lynkow/next/server'
import { config } from '@/lib/config'
export async function addToCart(variantId: string, quantity: number, locale: string) {
const client = createBuyerClient(config, await cookies(), { locale })
// Sent with the server key and the buyer's IP, read from the request
return client.cart.addLine({ variantId, quantity })
}The client forwards on every write (
POST,PUT,PATCH,DELETE) and onorders.getGuest(), the one read with a per-buyer limit. Other reads never read the request headers, so their pages can stay static.At build time or in a script, there is no request, so nothing is forwarded.
Passing
serverKeyorclientIptocreateBuyerClient()turns the automatic forwarding off for that client.serverKey: ''turns it off without sending anything.Create one buyer client per request. Never set
clientIpon a shared client such aslynkow(locale): every buyer would be counted as the first one.
A client you build with createClient() takes the pair from resolveBuyerForwarding(), which returns { serverKey, clientIp }, or {} when there is nothing to forward:
'use server'
import { headers } from 'next/headers'
import { createClient } from 'lynkow'
import { resolveBuyerForwarding } from '@lynkow/next/server'
export async function signIn(email: string, password: string) {
const lynkow = createClient({
siteId: process.env.NEXT_PUBLIC_LYNKOW_SITE_ID!,
publishableKey: process.env.NEXT_PUBLIC_LYNKOW_PUBLISHABLE_KEY,
...resolveBuyerForwarding(await headers()),
})
return lynkow.customers.login({ email, password })
}Calling headers() makes that route dynamic. In a Server Action it already is.
Set up another server stack
Send two headers on every call your server makes for a buyer, at least on the calls of the limits table:
curl -X POST "https://api.lynkow.com/storefront/$SITE_ID/customers/login" \
-H "X-Lynkow-Pk: lkw_pk_..." \
-H "X-Lynkow-Server-Key: lkw_srv_..." \
-H "X-Lynkow-Client-Ip: 203.0.113.7" \
-H "Content-Type: application/json" \
-d '{"email":"[email protected]","password":"..."}'X-Lynkow-Client-Ipholds one IPv4 or IPv6 address: no port, no list.Take it from a header your proxy or CDN overwrites on every request. With
X-Forwarded-For, read from the right and skip your own internal hops. Never take its first entry: the buyer can write it.Never send an internal address (
10.x,192.168.x,127.0.0.1). The API accepts it, and every buyer then shares that address.A missing, unknown or revoked key, or a value that is not an IP address, never fails the call. The API then counts your server.
Browsers cannot send these two headers to the API. Calls from a page need neither.
Choose where the buyer IP comes from
@lynkow/next reads the address from X-Forwarded-For: the first public entry from the right, past the private hops your host appends. Behind a CDN, that entry is the CDN's address, which every buyer it serves shares. LYNKOW_CLIENT_IP_HEADER names one header to read instead.
Your setup | Set |
|---|---|
A host with its own proxy, no CDN | No |
A CDN in front of your host | Yes, to the header your CDN overwrites with the visitor's address |
A host that documents its own client-IP header | Yes, to that header's name |
The two-network check fails | Yes, to the header your CDN or host overwrites with the visitor's address |
Your Next.js server faces the internet with no proxy | Do not forward: the visitor writes the whole |
Name only a header your CDN or host overwrites on every request, such as x-real-ip on hosts that set it. A header the buyer can send unchanged lets them choose the counter they are limited by. A value holding a comma, or one that is not an IP address, forwards nothing.
Check that it works
Last used. After a buyer write, such as adding a product to the cart, reload Settings > Developers > Storefront. The key's Last used column shows a date, within a minute of the call.
It moves only when the key and a valid IP address arrive together.
It shows the key arrives, not that the address is the buyer's. The next check proves that.
It never moves when an API token is used in place of the server key.
Two networks. This check pauses sign-in for the network you test from. On a site that does not forward, it pauses sign-in for every buyer for 15 minutes, so run it on a preview deployment or at a quiet hour.
From a phone on mobile data, with wifi off, try to sign in 10 times with 10 different made-up email addresses. The 11th attempt is refused with "too many attempts".
From another network, such as your office wifi, sign in with a real account.
If the sign-in works, each buyer has their own counter. If it is refused too, all buyers share one counter: see the troubleshooting table.
Troubleshooting
Symptom | Cause | Fix |
|---|---|---|
Sign-in refused for every buyer for 15 minutes after one person's mistakes | Your server forwards no buyer IP | Set |
Checkout refused for everyone after about 10 orders in 15 minutes | Same | Same |
429 on cart changes for everyone at busy times | The 120 per minute are shared | Same |
Last used says Never | The variable is missing from the deployed environment, no buyer has written yet, the key was revoked or regenerated, or it belongs to another site | Check the host's variables, add a product to the cart, copy the current key of this site |
Last used says Never with an API token in | An API token never updates this column | Create a server key and use it instead |
Last used says Never on a site still on | Those versions do not forward on their own | |
Last used moves, yet the two-network check fails | The address sent is your CDN's, your host's own or an internal hop | Set |
Everyone shares a counter behind a CDN | The first public | Same |
| The key is in client code | Move the call to the server, then regenerate the key |
The key is in a | The key reached the browser | Rename the variable, regenerate the key, redeploy |
Security
Keep the key on the server: a server-only environment variable, never a
NEXT_PUBLIC_name, never in a client component. The SDK throws when it findsserverKeyin a browser.The key only vouches for buyer addresses. Whoever holds it can choose the address they are counted under, and so get past the sign-in limits. Regenerate it as soon as it leaks.
Never forward an address read from a header the buyer controls, for the same reason.
Prefer a server key to an API token in
LYNKOW_SERVER_KEY.
Rotate or revoke a key
Regenerate keeps the name and replaces the value. Put the new value in your environment and redeploy right after.
Revoke removes the key. The shop keeps working, but all buyers share one counter again.
Both take effect within a minute. A site may revoke its last server key: it needs none to work.
Upgrade an older site
Your site | What to do |
|---|---|
Generated with | Nothing to update. Set |
Generated with | Update to |
Generated with | Update both packages, replace that file as shown below, then set |
Your own code passing | Nothing changes. To use the package's resolution, pass |
An API token in | Create a server key, swap it in, redeploy |
npm install @lynkow/next@^0.7.3 lynkow@~1.65.6A minor version of @lynkow/next can change behavior. Coming from a version before 0.7, read its changelog first.
A site generated with create-lynkow-app 0.2.12 passes its own buyer IP to every write, which turns off the automatic forwarding of @lynkow/next for those clients. Replace the whole of src/lib/buyer-forwarding.ts with this file, the one create-lynkow-app 0.2.13 generates, so the package forwards on its own:
import type { Client } from 'lynkow'
import { cookies } from 'next/headers'
import type { BuyerCookieReader, BuyerForwarding } from '@lynkow/next/server'
import { buyerClient } from './buyer'
export type { BuyerForwarding }
/**
* The buyer client of a server action that writes. @lynkow/next forwards the
* buyer's IP with LYNKOW_SERVER_KEY on its own.
*/
export async function actionBuyerClient(locale: string, store?: BuyerCookieReader): Promise<Client> {
return buyerClient(store ?? (await cookies()), locale)
}LYNKOW_CLIENT_IP_HEADER keeps the same name and meaning. Then run the two-network check.
Next Steps
Customer Accounts - sign-in, sign-up and their errors
Cart and Checkout - the per-request buyer client
Error Handling & Resilience -
TOO_MANY_REQUESTSand retriesStorefront: Site Access & Spam Protection - the headers in the API reference