useExperienceApi
The optional adapter that turns an (operationId, payload) function bridged to the platform store into a set of typed named operations the experience flow can dispatch by name.
Built-in pages emit data/events; the app supplies their platform-service actions and any host tasks. The flow looks at the page's action (either declared via withExperienceDefaults or set on the page def) and dispatches the matching adapter operation. A LoadingPage task can also run app-supplied service work. This adapter does not authenticate a caller or establish endpoint permissions.
Setup
Use the standard App scaffold’s @/stores/gxpPortalConfigStore import so the production bundle uses the installed host store. The reviewed AI scaffold’s @gx-runtime variant can bundle a separate local store; do not substitute it.
import { useGxpStore } from '@/stores/gxpPortalConfigStore'
import { useExperience, useExperienceApi, CameraReviewPage, FinalPage } from '@gxp-dev/app-ui'
const store = useGxpStore()
const callApi = (operationId, payload) =>
store.callApi(operationId, 'experience_api', payload)
const api = useExperienceApi({ callApi })
useExperience({
api,
pages: [
{ name: 'review', component: CameraReviewPage }, // → api.publishPost(data) → ctx.post
{ name: 'final', component: FinalPage },
],
})
The adapter accepts two arguments; the platform store requires three: callApi(operationId, dependencyIdentifier, data). Do not pass the store method directly or use an empty dependency identifier. In this example, experience_api must be a declared dependency in app-manifest.json containing every operation used by the flow. Replace it with your actual dependency identifier. For operations owned by different dependencies, resolve each operation to its declared identifier in the bridge and reject missing bindings.
The default endpoint strings below are adapter operation names, not a promise that the platform exposes matching operation IDs. Use endpoints to map each used action to the exact operation key in its manifest dependency. Keep authorization failures visible; do not fall back to mocks or raw HTTP when a live binding is missing or denied. Without api, named actions error out at runtime — but inline function actions (action: async (data) => …) still work.
Operations
The default methods unwrap a { data } envelope when present. publishPost, processImage and analyzeSketch handle FormData as described below; the other default methods forward their payloads. Overrides own their encoding and response normalization.
| Method | Default endpoint | Payload |
|---|---|---|
publishPost | social_stream.createPost | { blob?, caption?, attendee_id?, poster_name?, metadata? } |
fetchPost | social_stream.getPost | (id, { include? }) |
fetchRecentPosts | social_stream.getRecentPosts | — |
tagPost | social_stream.updatePost | (id, tags[]) |
createPrintJob | print_job.create | { social_stream_post_id, access_point_id? } |
processImage | ai_interface.processImage | { blob, prompt?, background_remover?, background_generator? } |
refinePrompt | ai_interface_prompt.refinePrompt | (prompt: string) |
generateImages | ai_interface_image.generateImages | { prompt, count? } |
analyzeSketch | ai_interface_text.analyzeSketch | { blob } |
call | any | (endpoint, payload?) — escape hatch |
Overrides
Replace a single operation
Useful when migrating from a legacy backend or stubbing in tests. The current implementation honors overrides for the named operations in the table; replace the bridge through callApi, because overrides.call does not replace the adapter's internal call function:
const api = useExperienceApi({
callApi,
overrides: {
publishPost: async (data) => {
const response = await fetch('/legacy/posts', { method: 'POST', body: data.blob })
if (!response.ok) throw new Error(`Publish failed (HTTP ${response.status})`)
return await response.json()
},
},
})
This fragment targets an app-owned legacy endpoint. Supply that service's authentication, validate its request/response schema and normalize any envelope yourself. fetch() resolves on HTTP rejection, so the explicit status check keeps rejected writes in the flow's error state. This override is not a fallback for a denied GxP binding.
Remap endpoints
Keep the defaults' behavior but point a method at a different endpoint string:
const api = useExperienceApi({
callApi,
endpoints: {
publishPost: 'social_stream_v2.createPost',
createPrintJob: 'badging.print_job.create',
},
})
The call escape hatch
For one-off endpoints that don't warrant a typed operation, use api.call:
{ name: 'enrich', component: MyEnrichPage,
action: async (data, _ctx, api) => api!.call('enrichment.run', { id: data.id }) }
call still unwraps the { data } envelope but doesn't FormData-encode — pass your own FormData if you need it.
Behavior details
Blob → FormData
publishPost converts to FormData when a top-level value is a Blob or File; otherwise its object passes through unchanged. processImage and analyzeSketch always construct FormData. Nested objects in a FormData payload are JSON.stringify-ed under their key.
Envelope unwrapping
The platform's typical response shape is { data: T }. Every default method returns T; an override supplies its own normalization. If the response doesn't have a data key, it's returned as-is.
Errors
Any rejected promise bubbles up through flow.next() → sets flow.error → keeps the page mounted. You can intercept globally via useExperience({ onError }), or per-page via an inline function action:
{
name: 'review',
component: CameraReviewPage,
action: async (data, ctx, api) => {
try {
return await api!.publishPost(data)
} catch (err) {
reportSentry(err)
throw err // re-throw to set flow.error
}
},
}
Typing
import type { ExperienceApi, CallApi, UseExperienceApiOptions } from '@gxp-dev/app-ui'
ExperienceApi is the full interface returned by useExperienceApi. CallApi is the shape of the underlying function: <T>(endpoint: string, payload?: unknown) => Promise<T>.
See also
- Overview — flow, context, actions, slots.
- Pages reference — which built-in pages have default actions wired up.
Named flow action payloads
The adapter methods retain their positional APIs. When dispatching them by page action name, fetchPost accepts either an ID or { id, include?: string[] }; tagPost requires { id, tags: unknown[] }. The flow converts those objects into the adapter's positional arguments and rejects malformed payloads before making a request. Other named actions receive the page's emitted payload as their first argument. Use an inline action when a page's output needs additional normalization.