Architecture and Concepts
An Embedded app has up to three parts: the iframe that Hootsuite loads in the dashboard, the Hootsuite host page around it, and your own backend. This page explains how they communicate and who holds which credentials.
Hootsuite dashboard (host)
│
│ postMessage: handshake, surface context, provider access token
▼
Your app (iframe, served from https://<appId>.hootsuite-apps.com)
│
│ HTTPS, Authorization: Bearer <provider access token>
▼
Your backend (BACKEND_HOST)
│
│ HTTPS, Authorization: Bearer <provider access token>
▼
Provider API (for example, Todoist)
The iframe
Your app is a single-page React app bundled into one file, app.js. Hootsuite serves it from its own origin for your app, https://<appId>.hootsuite-apps.com, and loads it in an iframe on each surface you selected.
Because the iframe runs on its own origin:
- It has no Hootsuite session and must not call Hootsuite APIs.
- It talks to the dashboard only through
postMessagemessages. - It talks to the outside world only through your backend.
The template uses a hash router (createHashRouter), so routes look like #/home. This works inside an iframe without any server-side routing.
The host handshake (AppConnector)
The template includes a small message bridge, AppConnector (src/context/AppConnector.ts). Treat it as platform infrastructure: you do not need to change it for normal app development. Your code reads its state through the useHootsuite() hook instead.
Message format
Every message is a JSON string with this envelope:
{
"type": "AppConnector",
"event": "appReadyRequest",
"payload": {},
"timestamp": 1760000000000,
"id": "msg_1760000000000_<YOUR_APP_ID>"
}
The bridge ignores messages whose type is not AppConnector. It also ignores any message whose origin is not exactly HOOTSUITE_DASHBOARD_URL, and uses that same value as the targetOrigin of every message it sends.
Startup sequence
- Your app mounts
HootsuiteContextProvider, which sendsappReadyRequestto the host. - The host answers with
appReadyResponse. The payload contains:context: the data for the current surface.authentication.accessToken: the user's provider access token, if the user has already connected your provider.
- The provider sets
isAppReadytotrue. If a token arrived,accessTokenis set andisAuthenticatedbecomestrue.
Wait for isAppReady before you read appContext or decide whether the user is signed in. The template's Entrypoint page does this and then redirects to #/authorize or #/home.
Messages
The iframe sends:
| Event | Payload | Sent when |
|---|---|---|
appReadyRequest | none | The app starts. |
authorizationRequest | none | Your code calls requestAuthorization(), for example when the user clicks Connect or Reconnect. |
accessTokenRequest | none | Your code calls requestAccessToken(). useBackend() does this after a 401. |
contextUpdateRequest | none | Your code calls requestContextUpdate(). |
errorReport | { kind, code? } | The template detects an error, or your code calls reportError(). See Report errors to Hootsuite. |
The iframe handles these events from the host:
| Event | Payload | Effect in the template |
|---|---|---|
appReadyResponse | { context, authentication?: { accessToken? } } | Sets appContext, sets accessToken if present, and sets isAppReady to true. |
isAuthorizing | { isAuthorizing } | Sets isAuthorizing. The host sends true when the consent flow starts and false when it ends, whether it succeeded or failed. |
authorizationResponse | { accessToken } | Sets accessToken and clears needsReconnect. |
authorizationError | { error } | Sets appError. |
accessTokenResponse | { accessToken } | Resolves the pending requestAccessToken() call and sets accessToken, unless it is the token the provider just rejected. |
accessTokenError | { error } | Rejects the pending requestAccessToken() call. It does not set appError. |
contextUpdateResponse | { context } | Replaces appContext and resolves the pending requestContextUpdate() call. |
Request a token or context on demand
By default, the host sends the token and the surface context once, in appReadyResponse. If your app stays open, it can ask the host for either one again. The template wraps both requests in helpers that return a promise:
requestAccessToken() sends accessTokenRequest.
- Use it when your app is open and the provider rejects the token, which your backend reports as
401.useBackend()already does this for you (see Call your own backend). - The host always answers, with
accessTokenResponseand{ accessToken }, or withaccessTokenErrorand{ error }when it has no token to give, for example because the user never connected the provider or renewal failed. Before answering, Hootsuite renews the token if it expires within the next minute (see Token renewal). - The promise resolves with the token or rejects with the host's error. As a safety net, it also rejects if no answer arrives within 8 seconds. Concurrent calls share one request, so several failed requests at the same time cause a single renewal.
- The host can return the same token. If the user revoked access in the provider before the token's expiry time, Hootsuite still considers the token valid and returns it again. Treat a token equal to the one that was just rejected as a failed renewal.
useBackend()does this.
requestContextUpdate() sends contextUpdateRequest.
- Use it when your app needs the context the host holds for the current session again, for example to restore its state. The host never sends the context on its own after the first
appReadyResponse: your app has to ask. - The host answers with
contextUpdateResponseand{ context }. The context has the same shape as inappReadyResponse(see Surface context reference). The promise resolves with the context, also updatesappContext, and rejects if no answer arrives within 8 seconds. - Do not use it to follow what the user does in the surface. When the surface context changes, the host loads your app again and sends the new context in a new handshake. In Perch, interacting with the calendar currently closes the drawer instead. See Context changes.
Report errors to Hootsuite
The template tells Hootsuite when your app fails for its users, with the errorReport message. Hootsuite uses it to see that an app is failing; it does not show these reports to you, and it never answers them.
The payload has two fields:
{ "kind": "request_failed", "code": 503 }
| Field | Rules |
|---|---|
kind | Required. One of crash, request_failed, auth_error, context_error or unknown. |
code | Optional. An integer from 0 to 999, usually the HTTP status. Leave it out, or send null, when there is no code, for example on a network error or a timeout. |
What each kind means, and when the template sends it for you:
kind | Sent automatically when |
|---|---|
crash | A component throws while rendering, a handler registered with on(...) throws, an exception is not caught, or a promise rejection is not handled. Errors while modules load, before src/index.tsx runs, are not reported. |
request_failed | A useBackend() request fails. code is the HTTP status; there is no code for a network error or a timeout. The 401 that leads to Reconnect, a request without a token and a request you aborted yourself are not reported. |
auth_error | Authorization finishes without a token. A member who cancels or declines the consent popup is not an error. Use it for authentication failures of your own, such as your backend rejecting your app's session. |
context_error | requestContextUpdate() fails or the host does not answer in time. |
unknown | Never sent automatically. Use it for anything else. |
To report your own errors, call reportError(kind, { code, error }) from src/context/errorReport.ts. It works inside and outside React:
import { reportError } from '@/context/errorReport';
reportError('request_failed', { code: response.status, error });
reportError('unknown');
Things to know:
- Only
kindandcodeare sent. Messages, stacks and other data never leave your app, so do not put personal data anywhere in a report. Hootsuite ignores any other field in the payload. - Hootsuite drops invalid reports whole. An unknown
kind, or acodethat is not an integer from 0 to 999, discards the report.reportErrorchecks this first, drops the report and logs a warning in the console. - The host does not answer and never throws. There is no response to wait for, and a report cannot break your app.
- Limits per session. Hootsuite records each distinct
kindandcodepair once per session, and at most 50 reports per session. Repeated errors do not add records, so you do not need to throttle. Report each occurrence once. - Pass the original
errorwhen you have it. It is never sent, but it stops the same error, or one that wraps it ascause, from being reported twice. - Do not report what Hootsuite already knows. Skip
authorizationErrorandaccessTokenError, which Hootsuite itself sends on the bridge. - Errors that are part of a normal flow. Call
excludeFromCrashReports(error)so they are not reported as acrashif they go unhandled. In your own error boundaries, callreportCrash(error).
The useHootsuite() hook
Call useHootsuite() in any component under HootsuiteContextProvider:
import { useHootsuite } from '@/context/useHootsuite';
const { isAppReady, isAuthenticated, needsReconnect, appContext, requestAuthorization } =
useHootsuite();
| Field | Type | Description |
|---|---|---|
isAppReady | boolean | true after the host answers the handshake. |
isAuthenticated | boolean | true when accessToken is set. |
accessToken | string | undefined | The provider's access token (not a Hootsuite token). |
isAuthorizing | boolean | true while the host runs the consent flow. |
needsReconnect | boolean | true after the provider rejected the token and the host could not renew it. The user has to reconnect. |
requestAuthorization | () => Promise<void> | Asks the host to open the provider's consent flow. Call it only from a user action, such as a click: browsers can block a popup that opens without one. It resolves as soon as the request is sent, not when the flow ends. |
requestAccessToken | () => Promise<string> | Asks the host for the current provider token. See Request a token or context on demand. |
requestContextUpdate | () => Promise<EmbeddedAppContext> | Asks the host for the current surface context. See Request a token or context on demand. |
requireReconnect | (rejectedToken?: string) => void | Drops the rejected token (the current one by default), so isAuthenticated becomes false, and sets needsReconnect. If the host later returns that same token, it is ignored. useBackend() calls it for you; call it yourself if you reach your backend another way. |
appError | string | The last error the host reported for the consent flow. |
setAppError, setAccessToken | setters | Update appError or accessToken from your own code. setAccessToken also clears needsReconnect. |
appContext | EmbeddedAppContext | undefined | The context of the current surface. |
currentLocale | string | Read from a locale URL parameter. Hootsuite does not send one today, so it is always en-US. Do not rely on it to detect the user's language. |
Hootsuite does not send the surface as a URL parameter. Detect the surface from the shape of appContext (see Surface context reference).
Surface context
appContext holds data from the surface where the app is open. Its shape depends on the surface: for example, Perch sends selectedDate, and Nest sends conversationId. Narrow the type with an in check before you read a field:
const selectedDate =
appContext && 'selectedDate' in appContext ? appContext.selectedDate : undefined;
See Surface context reference for every field.
Authentication: OAuth brokered by Hootsuite
Your app does not run OAuth itself. You register your provider's OAuth 2.0 settings in the Developer Portal, and Hootsuite runs the flow for each user:
- On startup, the host sends the user's provider token in
appReadyResponse, if the user has already connected your provider. The user does not reconnect each time, even when the token they received earlier has expired (see Token renewal). - Otherwise, the template shows a Connect button. Clicking it calls
requestAuthorization(), and the host opens the provider's consent screen in a popup.isAuthorizingistruewhile the flow runs. - After the user consents, the host sends
authorizationResponsewith the provider's access token. - If the flow fails, for example because the provider rejected the request, the user closed the popup, or the browser blocked it, the host sends
authorizationErrorand thenisAuthorizing: false. The template stores the error inappError, and the user can click Connect again. - Your app sends the token to your backend, which calls the provider's API with it.
Keep these points in mind:
- The client secret stays in the Developer Portal. It never reaches the iframe or your backend.
- The authorization is tied to the app, not to the URL the app loads from. Switching between a development URL and an uploaded bundle does not require users to reconnect.
- Never open the consent flow automatically, for example from a failed request or when the page loads. A popup that does not follow a click can be blocked by the browser. Call
requestAuthorization()only from a Connect or Reconnect button. - If the provider rejects a token while the app is open, the template first asks the host for a renewed token and shows Reconnect only if that fails. See Token renewal.
Token renewal
Many providers issue short-lived access tokens together with a refresh token.
Hootsuite stores the refresh token and renews the access token for you:
- Whenever the host hands the token to your app, Hootsuite first checks whether it expires within the next minute. If it does, Hootsuite exchanges the refresh token for a new access token with your provider, saves the result, and sends the new token. This includes the startup handshake and any
accessTokenRequest. - If the provider rotates the refresh token on every renewal, Hootsuite keeps the new one.
- Your iframe and your backend never see the refresh token or the client secret.
- Some providers only issue a refresh token when you request offline access, for example with the
offline_accessscope. Check your provider's documentation and set the scope in the Developer Portal.
Keep these limits in mind:
- Hootsuite renews a token only when someone asks for it. It does not renew tokens in the background and does not push new tokens to your app. Your iframe holds the token it received at startup, so if the app stays open longer than the token lifetime (often one hour), that token expires and the provider rejects it. To get a renewed token without a new authorization, call
requestAccessToken()(see Request a token or context on demand). - Renewal can fail. The refresh token may have expired, the user may have revoked your app in the provider's settings, or the provider may not have issued a refresh token. At startup, the host then sends no token and the user sees Connect. While the app is open, the host answers
requestAccessToken()withaccessTokenError, or returns the same token the provider just rejected.
When the provider rejects the token while the app is open, the template's useBackend() handles it in this order:
- Your backend returns
401. useBackend()callsrequestAccessToken(). If the host returns a different token, it repeats the request once with that token.- If the host reports an error, does not answer within 8 seconds, returns the same token, or the repeated request also gets a
401,useBackend()callsrequireReconnect()and throws anUnauthorizedError. needsReconnectbecomestrueandisAuthenticatedbecomesfalse, so the template shows the Authorize page with a Reconnect button. The consent popup opens only when the user clicks it.
See Call your own backend for the details.
Disconnecting. A user can disconnect your provider in two ways: by revoking your app's access in the provider's own settings, or by uninstalling your app from Hootsuite, which removes the authorization stored for that user. In both cases, the next time the app opens, the host sends no token and the user sees Connect again.
Your own backend (optional)
If your app only displays the surface context, it does not need a backend. If it reads data from a provider, it needs one, because the iframe cannot call the provider's API with credentials directly and must not hold secrets.
Your backend:
- Receives requests from the iframe with
Authorization: Bearer <provider access token>. - Calls the provider's API with that token.
- Allows CORS requests only from your app's origins.
- Runs on a public HTTPS URL once the bundle is hosted by Hootsuite.
Your backend is not part of app.js. You deploy and operate it yourself. See Call your own backend.