No SvelteKit SDK, so I am calling the Content API from a server load function. Sane?
Couldn't find a SvelteKit SDK so I prototyped this in a +page.server.js load function and it works, but I want a sanity check before building on it:
export async function load({url}) { const response = await fetch('https://api.croct.io/external/web/content', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Api-Key': CROCT_API_KEY, }, body: JSON.stringify({ slotId: 'home-hero', version: '1', context: {page: {url: url.href}}, }), });
return {hero: await response.json()};}Is the request shape right? And what am I actually missing versus a real SDK, is it mostly convenience or is there behavior I would have to rebuild myself
1 answer
Your shape is correct. POST /external/web/content with the X-Api-Key header is the right endpoint, and page.url inside context is required, which you have. You can also pass preferredLocale and context.attributes in the same body when you need them.
One thing worth using: the response carries metadata including contentSource (whether the content came from the slot default, an experience, or an experiment), plus experienceId, experimentId, and variantId. So experiment participation is fully visible server-side, useful if you want to forward the variant to your analytics from the same load function.
What an SDK would add on top: generated TypeScript types for slot content, client-side session and event tracking, and the plumbing around user tokens. For server-rendered content alone, the API path you have is complete, and since the content arrives in the initial HTML there is nothing to hide or swap on the client.
There is no SvelteKit SDK today. If you want one, register interest through the feature-request route on the integrations page; that is how new SDKs get prioritized.