Hand a token to the quote form#
The quote form normally starts with its own key. To have it use your API
application instead, for example to offer any of your live products on one page,
give it getToken. The browser must never see your client secret, so set it up
like this:
- Create a browser application. Tick
readandwrite, and list your website under Allowed origins (see Create your API applications). - Add a small token endpoint to your own server, such as
/subcontext-token. It asks Subcontext for a token with the application's client id and secret, and returns only{ "access_token": "...", "expires_in": 7200 }. token-server.mjs (Node), token-server.php (PHP) anddotnet token-serverin Program.cs (C#) are working examples. Each keeps one token for all visitors until it nearly runs out, and tells each page how many seconds are left. - On your page, give the quote form a
getTokenthat calls your endpoint:
<div id="quote"></div>
<script src="https://js.subcontext.com/quoteform-latest.js"></script>
<script>
Subcontext.Quote.init({
container: '#quote',
productSlug: 'YOUR_PRODUCT_SLUG',
journeySlug: 'YOUR_JOURNEY_SLUG',
getToken: () =>
fetch('/subcontext-token').then((response) => {
if (!response.ok) throw new Error('Could not get a token (' + response.status + ')')
return response.json() // { access_token, expires_in }
})
})
</script>
The form asks again 30 seconds before the token runs out, and once more if
Subcontext answers 401. Put the endpoint on the same website as the page, so
the browser needs no extra cross-origin setup.
The whole quote form, with all its options, is in the quote form guide. The example page here is quote-form-with-your-token.html.
-
token-server.mjs
Node: a small token endpoint for your pages, GET /subcontext-token.
-
token-server.php
PHP: a small token endpoint for your pages, for PHP's built-in web server or your own.
-
quote-form-with-your-token.html
The quote form with getToken calling your token endpoint.
Use a token in your own JavaScript#
Your own page scripts can use a browser application's token the same way: get
it from your token endpoint and send it in the Authorization header.
const { access_token } = await fetch('/subcontext-token').then((r) => r.json())
const products = await fetch('https://subcontext.com/api/v1/products', {
headers: { Authorization: 'Bearer ' + access_token }
}).then((r) => r.json())
The page must be on one of the application's allowed origins. The browser adds
the Origin header and Subcontext checks it. The example page
call-the-api-from-the-browser.html
lists your products this way, keeps the token until shortly before it runs
out, and tries once more after a 401.
-
call-the-api-from-the-browser.html
Your own JavaScript listing your products with a token from your token endpoint.
A browser application's token is one anyone can get, so it doesn't reach an
application that already exists on its own. Keep the share_token from the
answer that started the application, and send it in an X-Share-Token
header with every later request about the application or its quotes:
const answers = await fetch('https://subcontext.com/api/v1/applications/' + application.id + '/answers', {
method: 'PATCH',
headers: {
Authorization: 'Bearer ' + access_token,
'Content-Type': 'application/json',
'X-Share-Token': application.share_token
},
body: JSON.stringify({ answers: { smoker: false } })
}).then((r) => r.json())
Without it, or with another application's, Subcontext answers
404 not_found, as it does for an id that doesn't exist. The quote form
sends it for you.
To try either page on your own computer, see Trying the pages.