Skip to content

Ask for a decision

Your site asks for features by name, up to 32 in one request, and gets one decision for each. A feature never runs until a request names it, so saving or deploying a feature changes nothing on a page that does not ask for it.

There are two routes, and each takes one kind of credential:

Route Who calls it Credential Who the decision is for
POST /edgekit/v1/decide Your pages, through /edgekit/ on your own domain None. The page’s origin must be in the site’s allowed origins The user the sign-in cookie names, else the anonymous device
POST /v1/decide Your servers An API key pinned to the site The userId your server sends, else an anonymous device

Both routes take the site in the x-edgekit-site-id header and answer the same way. The full schema is in the decide API reference.

The browser client, createClient, sends this request for you with its decide method. Without it, post to /edgekit/v1/decide on your own domain, so the sign-in cookie goes with the request:

const response = await fetch('/edgekit/v1/decide', {
method: 'POST',
credentials: 'include',
headers: {
'content-type': 'application/json',
'x-edgekit-site-id': 'site_example',
},
body: JSON.stringify({
features: ['paywall', 'subscribe-prompt'],
context: { article_id: 'a-1', section: 'news' },
}),
});
const { decisions, unknown } = await response.json();

The same request with cURL, against your tenant host. It carries no cookies, so it is decided for an anonymous device:

Terminal window
curl -X POST https://<tenant>.edgekit.ai/edgekit/v1/decide \
-H 'content-type: application/json' \
-H 'x-edgekit-site-id: site_example' \
-H 'Origin: https://www.example.com' \
-d '{"features":["paywall","subscribe-prompt"],"context":{"article_id":"a-1","section":"news"}}'

This route refuses a userId with 403 forbidden, in the body or in the query string. The user comes from the sign-in cookie and nowhere else. Setting up /edgekit/ on your domain is covered in Mount EdgeKit on your site’s own domain.

Send an API key pinned to the site, as Authorization: Bearer <key> or in x-api-key. To decide for a signed-in user, send their id as userId:

Terminal window
curl -X POST https://<tenant>.edgekit.ai/v1/decide \
-H 'authorization: Bearer YOUR_API_KEY' \
-H 'content-type: application/json' \
-H 'x-edgekit-site-id: site_example' \
-d '{"features":["paywall"],"userId":"0199a4c2-7b1e-7c3d-9a4f-2e8b5d6c1a90"}'
const response = await fetch('https://<tenant>.edgekit.ai/v1/decide', {
method: 'POST',
headers: {
authorization: `Bearer ${process.env.EDGEKIT_API_KEY}`,
'content-type': 'application/json',
'x-edgekit-site-id': 'site_example',
},
body: JSON.stringify({ features: ['paywall'], userId }),
});

EdgeKit takes userId at face value and reads that user’s current grants. Take it from a sign-in your server has checked, never from a value on the incoming request. Without userId, the decision is for an anonymous device. createServerClient is the server client that sends this request.

A Cloudflare Worker on the same zone as your tenant host cannot reach it with the global fetch. When EdgeKit runs in your own Cloudflare account, bind your Worker to the decide Worker with a service binding and pass its fetch:

const edgekit = createServerClient({
baseUrl: 'https://<tenant>.edgekit.ai',
apiKey: env.EDGEKIT_API_KEY,
siteId: 'site_example',
fetch: (input, init) => env.EDGEKIT_DECIDE.fetch(input, init),
});

A request over the binding does not pass your zone’s WAF or rate limiting rules.

At a site where users sign in only through your own identity provider, a userId is refused with 422 unprocessable_entity, This site cannot take an asserted userId. Send your own request’s Cookie header instead: EdgeKit reads your identity provider’s credential from it and decides for that user. A credential that is missing or does not verify gets an anonymous decision.

A server call does not carry what a browser sends, so forward it. EdgeKit reads these headers on the server route:

Header What reads it
cookie The device a meter counts for, and your identity provider’s credential
user-agent request.device_class
referer request.referrer and request.referrer_class
cf-ipcountry request.geo
accept-language, x-forwarded-for, cf-connecting-ip Forwarded with the others

Without them every user is decided as a desktop device with no referrer and no country, and every anonymous user shares one meter. The server client forwards them, and the page URL’s utm_ parameters, when you pass the incoming request:

const results = await edgekit.decide('paywall', { request });

Over a service binding, it also carries the request’s bot verdict, which the bot.* facts read.

When a decision exposed the request to one of EdgeKit’s own experiments, the server client records it for you. It posts one experiment_exposure event per entry of the response’s exposures to your collector, at /edgekit/a/t on baseUrl, in one request after each decide. Each event has a new event id and the unitId from the response as unit_id. The request carries no cookie and nothing else from the user’s request, so the event has no device cookie, IP address or location of its own.

decide does not wait for that request. Pass waitUntil so your runtime keeps it alive after you respond:

const results = await edgekit.decide('paywall', {
request,
waitUntil: (sent) => ctx.waitUntil(sent),
});
  • The promise never rejects. It resolves false when the collector did not accept the events within 2 seconds, and the decision is the same either way. Nothing is sent again.
  • Without waitUntil the request is still sent. On a runtime that stops work when the response is returned, such as Cloudflare Workers, it can be cut off.
  • Each event is a billed event: one per decide call for each experiment that call exposed.
  • A response with preview: true sends none.
  • When fetch is a service binding to the decide Worker, bind the collector Worker too and pass it as collectorFetch: (input, init) => env.EDGEKIT_COLLECTOR.fetch(input, init). The decide Worker does not take /edgekit/a/t, so without collectorFetch no exposure is recorded.
  • The request goes through the fetch you gave createServerClient. A service binding to the decide Worker does not reach the collector, so send the /edgekit/a/t request another way in that function, or the exposures are not recorded.

If your server already sends experiment_exposure from the decide response, remove that code, or every exposure is sent twice. See Exposures.

Field Required What it does
features Yes 1 to 32 feature names. A name asked twice is decided once
context No An object of facts about the page. See Page facts in context
content No Signed content facts. No rule can test them, and a request carrying them answers 503 unless EdgeKit has set them up for your organization. See Troubleshooting
userId No Server route only. Who the decision is for
giftLink No The page URL’s _gt value, as it is. See Gift links
newsletterLink No The page URL’s ek_nl value, as it is. Read only when the request is otherwise anonymous. See Newsletter links
castleRequestToken No A fresh Castle request token from castle.js, for the castle.* facts. Without one, a rule reading them routes to the feature’s error outcome. See Castle

An outcome preview’s token goes in the x-edgekit-preview header, or in the edgekit_preview cookie when there is no header. Without a valid one, the request is answered from the deployed document.

The body may be at most 64 KB. A body that does not match this shape gets 422 unprocessable_entity, Request validation failed.

{
"decisions": {
"paywall": {
"outcome": "metered",
"type": "allow",
"reason": "meter",
"meters": { "articles": { "count": 2, "allowance": 3 } }
},
"subscribe-prompt": {
"outcome": "promote",
"reason": "paywall",
"value": { "label": "Read in full" }
}
},
"unknown": [],
"preview": false,
"exposures": []
}
  • decisions has one entry for each feature that answered:
    • outcome is the id your page switches on. It is null, with reason error, when the slot that answered returns no outcome.
    • type is the outcome’s access type, when it has one: allow or deny say whether the page opens, and data and redirect are not about access. Treat a value you do not know as no type.
    • reason is the outcome’s reason, from a fixed set your reporting can group on. reasonDetail is your own word for it, when the outcome has one.
    • value holds the outcome’s fields. It is absent for an outcome with no fields. See Fields.
    • checkoutPageUrls holds the checkout page URL of each offer_token field whose outcome sells on a checkout page, keyed by the field’s name. The URL carries the token, so link to it as it is. It is absent when no field names a checkout page. See Sell an offer on a checkout page.
    • meters holds each meter the feature’s rules read, with the count and the allowance as read before this request counted anything. allowance - count is what was left. It is absent when no rule read a meter. On a preview pinned feature it still shows the user’s real reads, and nothing counts.
    • experience is what the site shows for this outcome, when the site has an experience for it: the module’s url and contract, the display, the trigger and the site’s brand. The browser client draws it when you create the client with experiences: true. It is absent when the site has none, and a client that does not show experiences ignores it. Treat a display or trigger kind you do not know as nothing to show.
  • unknown lists the names that got no decision: not deployed, left out of the deployment, or not running on this site. All three look the same, so a typo in one name never fails the others.
  • preview is true when the request carried a valid outcome preview token and was answered from the staged preview, and false otherwise. Only then does the response also carry previewHash, the hash of the preview that answered, and pin, the token’s pin with matched set to false when the preview has no such row. See Preview an outcome on your site.
  • exposures lists the experiment arms this request exposed, one per experiment, each with the unitId the arm was assigned to. Decide records none of them. The browser client sends each one as an experiment_exposure event through the tracker you gave it, and the server client and the site proxy send them to your collector. Each is a billed event, and none is sent when preview is true. If your page sends this event itself, remove that code. See Exposures.
  • staleIdentity is present and true when the request had a sign-in session but no usable access cookie, so it was decided with no grants. Call POST /edgekit/v1/id/get-session once to refresh the cookie, and the next decide uses it. The browser client does this for you in the background, at most once per client.

Each decide request counts on the meters its winning rules count. A retry of a request that already got an answer counts again on a count meter. A seen-set meter counts one key once, so asking again about the same article costs nothing. See How a meter counts.

By default, a user who holds any live grant counts on no meter except a gift link’s.

Route Limit
POST /v1/decide 2,000 requests per 10 seconds per API key
POST /edgekit/v1/decide 200 requests per 10 seconds per client per site
GET /v1/decide/forward 200 requests per 10 seconds per client per site

The browser route, /decide/forward on either path and creating a gift link from a page share one count per client per site. The API key routes, creating a gift link included, share one count per key.

Past the limit, decide answers 429 too_many_requests, Too many decide requests., with Retry-After: 10. The browser client waits 2 seconds for an answer and the server client 5. Neither retries for you, and neither throws for a failed decision: every feature you asked for answers unavailable, and your page shows what it shows for that state. See Troubleshooting.

context is the only way a browser can describe the page it is asking about, such as the article’s id or section. Rules read it as context.*, exactly as sent:

  • Anyone can set any value. A rule that opens a page because of a context value can be opened by anyone who sends that value. Stage warns on every rule that reads one.
  • It never mixes with signed content facts. context.section and signed.section are two different facts.
  • The console’s rule builder does not list context.* facts. A feature you author in the console tests only the facts in Revenue › Decision Engine › Facts.

No rule can test a signed content fact either, so no rule has a fact about the page that a user cannot change. See content in The request.

From a page, decide reads the user’s grants from a signed access cookie, not from your organization’s database. The grants in the cookie are read again whenever the page reads the sign-in session. Each decide extends the cookie by 15 minutes. It is refused 15 minutes after it was last written or extended, and in any case an hour after its grants were read. A grant that changes elsewhere, such as a purchase on another device or a revoked grant, reaches decisions in that browser the next time the page reads the sign-in session, and at most an hour later.

From your server, and at a site where users sign in only through your own identity provider, every request reads the user’s current grants.

A journey feature returns only redirect outcomes. Instead of asking for it with POST, your page sends the user to:

Terminal window
curl -i 'https://<tenant>.edgekit.ai/v1/decide/forward?feature=registered&site=site_example'

The route answers 302 with the destination in Location. Use it as a link or with location.assign, never with fetch. It is served under /edgekit/v1/decide/forward too, takes no API key and no origin check, and reads the user from the sign-in cookie. It refuses a userId with 403 forbidden. A journey feature counts on no meter, so following the link again costs nothing.

The destination must be on one of the site’s redirect origins. The console does not create journey features or redirect outcomes. A feature that is not a deployed journey feature on the site answers 404 not_found.

edgekit.ai