App Bridge
Build embedded apps that run inside the Cartly admin panel using iframe-based integration.
Architecture
Cartly App Bridge allows third-party apps to be embedded directly within the Cartly admin dashboard via iframes. When a merchant opens your app, Cartly loads your application URL inside a sandboxed iframe and passes authentication context.
Key features:
- Seamless integration within the Cartly admin UI
- A short-lived, app-scoped access token handed over by postMessage — no OAuth credentials in the browser
- Access to the App API with the scopes granted during installation
- Namespace-isolated metafields for app-specific data storage
Session Token Flow
When a merchant opens your embedded app, the Cartly admin posts an APP_BRIDGE_INIT message to your iframe with a short-lived access token scoped to your app and the shop, plus the API base URL to call. Your app answers APP_BRIDGE_READY and uses the token directly — there is nothing to exchange.
// 1. The Cartly admin opens your app's URL (its website_url) in an iframe and,
// once it loads, posts APP_BRIDGE_INIT with a short-lived access token scoped
// to your app and the shop. There is no token in the URL and no exchange step.
window.addEventListener("message", (event) => {
// Accept it only from the admin window that embeds you, on a Cartly admin origin.
if (event.source !== window.parent || !isCartlyAdminOrigin(event.origin)) return;
if (event.data?.type !== "APP_BRIDGE_INIT") return;
const { accessToken, shopId, locale, apiUrl } = event.data.payload;
// 2. Answer right away: the admin shows a handshake error if APP_BRIDGE_READY
// hasn't arrived 5 seconds after it opened your app (iframe load included).
window.parent.postMessage({ type: "APP_BRIDGE_READY" }, event.origin);
// 3. Call the App API on apiUrl (the shop's own API base) with the token.
startApp({ accessToken, shopId, locale, apiUrl });
});The merchant admin runs on https://cartly.pro, https://*.cartly.pro or https://*.mycartly.pro (regional deployments add their own admin host) — accept APP_BRIDGE_INIT only from window.parent on such an origin. *.mycartly.pro also serves storefronts, so the origin alone is no proof: treat the token as the credential and check its shop with GET /apps/api/me. The admin talks only to your app's registered origin (the origin of its Website URL), so that URL must load your app directly: a redirect to another origin breaks the handshake.
The token lasts 1 hour. When it expires (a 401 from the API), post { type: "APP_BRIDGE_TOKEN_REQUEST" } to window.parent; the admin answers APP_BRIDGE_TOKEN with a fresh accessToken in its payload, or APP_BRIDGE_ERROR with code token_refresh_failed or token_refresh_throttled (at most one refresh a minute).
App API Endpoints
Embedded apps access Cartly data through the App API. Each endpoint requires a valid OAuth access token and the appropriate scope.
| Method | Endpoint | Scope | Description |
|---|---|---|---|
| GET | /apps/api/products | products:read | List products |
| GET | /apps/api/orders | orders:read | List orders |
| GET | /apps/api/customers | customers:read | List customers |
| GET | /apps/api/collections | collections:read | List collections |
| GET | /apps/api/shop | shop:read | Get shop info |
| PUT | /apps/api/metafields/:owner_type/:owner_id/:namespace/:key | metafields:write | Create or update a metafield in your namespace |
| GET | /apps/api/metafields/:owner_type/:owner_id | metafields:read | List your app's metafields of a resource |
| POST | /apps/api/webhooks | webhooks:manage | Create webhook |
Namespace Isolation
Each app gets its own metafield namespace: app:{appId}. This means your app can store custom data on any entity (products, orders, customers, etc.) without conflicting with other apps or the merchant's own metafields.
- Your app can only read/write metafields within its own namespace
- The namespace is automatically enforced by the API — you cannot access other apps' data
- Merchants can view (but not edit) your app's metafields in the admin panel
Code Example
Here is a complete example showing how to fetch products and create a metafield from within an embedded app:
// Fetch products from within an embedded app
async function getProducts(apiUrl, accessToken) {
const response = await fetch(`${apiUrl}/products`, {
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
return data.data; // products; paging in data.meta
}
// Create or update a metafield in your app's namespace: app:<your app id>, the
// app's ID (a UUID, shown in its URL in the admin app settings), not its client_id.
// ownerType: product | variant | order | customer | collection | shop
async function setMetafield(apiUrl, accessToken, appId, ownerType, ownerId, key, value) {
const namespace = `app:${appId}`; // any other namespace is refused (403)
const response = await fetch(
`${apiUrl}/metafields/${ownerType}/${ownerId}/${namespace}/${key}`,
{
method: "PUT",
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ value, value_type: "JSON" }), // value: a JSON object
},
);
return response.json();
}