Use destinations in your app
Early access notice
The Destinations Delivery API is currently in early access while development is ongoing. Before integrating, please keep the following in mind:
- Performance and SLOs: Response times do not yet meet standard Contentful SLOs, and some queries have latencies too high for production delivery traffic. We are aware of this limitation and are actively working on performance optimizations. (Note: Fetching experiences by ID rather than destination does meet our response-time SLOs.)
- Potential breaking changes: Resource schemas, payload structures, and endpoints are subject to change prior to General Availability (GA).
Destinations can be consumed in the following ways:
- During static site generation.
- At render time using the client’s path.
- With a hard-coded path or destination node ID.
Each example in this guide is shown with the Experiences SDK, which wraps the API calls and resolves the experience for you, and with the underlying API requests. The SDK examples use @contentful/experiences-react.
Static site generation
To statically generate an entire site for a destination, first retrieve its sitemap.
Experiences SDK
Raw API
fetchDestinationSitemap lists the paths in a destination.
Each item has the shape { path, experienceId, type }. The SDK extracts experienceId from the API’s experienceUrn.
From here, there are two ways to retrieve each experience:
- Resolve each path through the Destinations Delivery API.
- Use the experience ID from the sitemap (the
experienceUrnin the raw response) and request it from the Experience Delivery API.
These approaches differ in how they use the destination context.
Using the Destinations Delivery API (recommended)
The Destinations Delivery API resolves an experience within its destination context. When an experience is published, its delivery artifacts are updated asynchronously. A destination node tracks which delivery artifacts belong to its linked experience, so resolving through the destination keeps the node and experience artifacts consistent.
Prefer this approach unless measured performance requirements justify bypassing the destination context.
With the SDK, pass the destinationId and a path from the sitemap to fetchExperience. The raw equivalent is the POST .../experiences request shown in Using the client’s path.
Using the Experience Delivery API
Resolving an experience directly through the Experience Delivery API avoids the destination tree lookup and may reduce latency, but it bypasses the destination context and its consistency guarantees. Consider this tradeoff on a case-by-case basis, and prefer the Destinations Delivery API when possible.
With the SDK, pass the sitemap item’s experienceId to fetchExperience. Unlike destination fetches, this call takes an environmentId:
Using the client’s path
Experiences can be resolved at request time, either server-side or client-side, using the client’s path.
For example, a site has the following pages:
The Destinations Delivery API can resolve the experience for the current path:
In a client-side React application, this could look like:
The notable parts of this example are:
fetchExperiencereads the path fromwindow.location.pathname. The pathname should exactly match a resolved destination node path.- A redirect node resolves to a
DestinationRedirectResult,{ redirect: { path } }, instead of an experience. This example navigates the browser to that path; alternatively, the application can callfetchExperienceagain with it. NotFoundErrormeans no destination node claims the path. Render your not-found state.
Because the fetch runs in the browser, the access token ships in your bundle. Use a token that’s safe to expose, or fetch on the server. See Fetch experiences.
The notable parts of this example are:
- The request reads the path from
window.location.pathnameand sends it to the Destinations Delivery API. The pathname should exactly match a resolved destination node path. - If the response is a redirect, it contains the target path. This example navigates the browser to that path; alternatively, the application can make another Destinations Delivery API request for it.
Hard-coding a path or destination node ID
The final option is to hard-code a path or destination node ID. A hard-coded path uses the same path-resolution endpoint shown above. A stable destination node ID can be preferable to an experience ID for two reasons:
- The upsert operation lets you choose a meaningful, stable destination node ID.
- The destination node can remain the same when its linked experience changes, so the client doesn’t need to be updated when editors replace the experience.
To create a destination experience node with a chosen ID, use the Destinations Management API upsert operation. This operation requires a Contentful management token, which must not be exposed in a client-side application:
Publish the destination node before requesting it from the Destinations Delivery API. Use the version returned by the upsert response, and make sure the destination is also published:
The client can then resolve the experience using the hard-coded destination node ID:
Experiences SDK
Raw API
Pass nodeId instead of path. Redirects and NotFoundError are handled the same way as in the previous example.
Examples
The examples directory in the Experiences SDK repository has runnable apps that serve a destination at the root path. Each example’s README explains how to seed a destination with its bootstrap script.