Skip to main content

Add a tab to the product page

In this tutorial you will add a tab to the product page that shows what your app knows about the product. You need an app that already installs: start from Run the sample app if you do not have one.

How it works​

  1. The platform opens your page in a frame, inside a tab of the product page.
  2. Your page asks App Bridge for a session token. For this slot, the token carries the product: slot is product.details.tab and ctx.productId is the product's id.
  3. Your page sends the token to your server, which verifies it and reads the product from the App API.

The product id comes from the verified token, never from the page's URL: a user cannot make your server read another product.

1. Declare the extension​

The extension needs an embedded app, an app_url, and the read_products scope:

{
"embedded": true,
"app_url": "https://app.example.com/app",
"scopes": ["read_products", "read_organizations"],
"extensions": [
{
"slot": "product.details.tab",
"path": "/app/extensions/product",
"label": { "en": "Carbon facts", "fr": "Infos carbone" }
}
]
}

The path is resolved against app_url. The extension slots reference gives the frame's heights and the token's context.

2. Allow the platform to frame the page​

Answer the page with a Content-Security-Policy that names the web origins of the platforms you trust:

Content-Security-Policy: frame-ancestors https://{tenant}.example.com

3. Write the page​

<!doctype html>
<meta charset="utf-8" />
<p id="line">Loading…</p>
<script type="module" src="./product.js"></script>
import { createApp } from '@foodpilot/app-bridge';

// The platforms' web origins, exactly: the same list as frame-ancestors.
const app = createApp({ allowedHosts: ['https://{tenant}.example.com'] });
app.autoResize(); // the tab follows the height of the document

await app.ready();
const response = await fetch('/api/extension/product', {
headers: { Authorization: `Bearer ${await app.getSessionToken()}` },
});
const line = document.getElementById('line')!;
line.textContent = response.ok ? `Product: ${(await response.json()).name}` : 'This information is not available.';

Write text with textContent, never innerHTML: nothing from the network is parsed as HTML.

4. Verify the token and read the product​

import { verifySessionToken, SessionTokenError } from '@foodpilot/app-bridge/server';

app.get('/api/extension/product', async (request, response) => {
const token = /^Bearer (\S+)$/i.exec(request.headers.authorization ?? '')?.[1];
if (!token) return response.status(401).json({ error: 'missing_token' });

let claims;
try {
claims = await verifySessionToken(token, {
apiOrigin: 'https://{tenant}-api.example.com',
appOrigin: 'https://{tenant}.example.com',
clientId: process.env.CLIENT_ID,
platform: installation.platform,
installationId: installation.id,
organizationIds: installation.organizationIds,
});
} catch (error) {
if (error instanceof SessionTokenError) return response.status(401).json({ error: error.code });
throw error;
}

const productId = claims.ctx?.productId;
if (claims.slot !== 'product.details.tab' || productId === undefined) {
return response.status(403).json({ error: 'wrong_slot' });
}

// With the installation's access token, kept on your server.
const product = await appApi(installation, `/api/app/v1/products/${productId}`);
response.set('Cache-Control', 'no-store').json({ id: productId, name: product.name });
});

installation is what you stored at install time; appApi calls the App API with its access token, as in Build your first app. A product outside the installation's organizations answers 404.

5. Try it​

Submit a version with the extension, then open a product on your test platform: the tab appears after the built-in ones. See Test your app.