Third-party API queries and caching in Hydrogen
This recipe integrates third-party GraphQL APIs into your Hydrogen storefront with Oxygen's powerful sub-request caching system. Using the Rick & Morty API as an example, you'll learn how to:
-
Create a cached GraphQL client - Build a reusable client factory that minifies queries, handles error handling, and integrates with Oxygen's caching infrastructure.
-
Integrate with Hydrogen's context - Add the third-party client to the global context system, making it available in all routes and actions throughout your application.
-
Query external APIs efficiently - Fetch data from third-party sources in parallel with Shopify API calls, leveraging Oxygen's caching to minimize latency and API calls.
Anchor to Use casesUse cases
This pattern is perfect for integrating:
- CMS platforms (Contentful, Sanity, Strapi)
- Review systems (Yotpo, Judge.me, Reviews.io)
- Analytics services (custom dashboards, reporting APIs)
- Custom backend APIs (inventory systems, ERP integrations)
- Marketing tools (email platforms, loyalty programs)
Anchor to Performance benefitsPerformance benefits
- Sub-request caching: Responses are cached at the edge, reducing API calls
- Parallel data fetching: Load third-party and Shopify data simultaneously
- Configurable cache strategies: Use CacheShort(), CacheLong(), or custom TTLs
- Automatic cache key generation: Based on query and variables
Anchor to Key featuresKey features
- Caching strategies can be customized per query using Hydrogen's cache utilities (CacheShort, CacheLong, CacheNone)
- The client is added to the global context, making it available in all routes
- TypeScript types are automatically augmented for full IDE support
- Error handling is built-in with graceful fallbacks
The examples in this recipe use rickandmortyapi.com for demonstration purposes, but the patterns work with any GraphQL or REST API.
The examples in this recipe use rickandmortyapi.com for demonstration purposes, but the patterns work with any GraphQL or REST API.
Anchor to RequirementsRequirements
- Basic knowledge of GraphQL
- Understanding of Hydrogen's context system
- Familiarity with TypeScript (for type augmentation)
Anchor to IngredientsIngredients
New files added to the template by this recipe.
| File | Description |
|---|---|
| app/lib/createRickAndMortyClient.server.ts | A GraphQL client factory for third-party APIs with Oxygen caching support |
Anchor to Step 1: Document third-party API integrationStep 1: Document third-party API integration
Add documentation explaining how to integrate external GraphQL APIs with Oxygen caching.
File
Anchor to Step 2: Create the third-party API clientStep 2: Create the third-party API client
Create a new GraphQL client factory that integrates with Oxygen's caching system. This client handles query minification, error handling, and cache key generation.
File
Anchor to Step 3: Add the client to Hydrogen contextStep 3: Add the client to Hydrogen context
Import the Rick and Morty client and add it to the Hydrogen context so it's available in all routes. Also update TypeScript declarations for proper type support.
File
Anchor to Step 4: Query and display third-party dataStep 4: Query and display third-party data
Update the homepage to fetch data from the third-party API and display it alongside Shopify data. This demonstrates parallel data fetching and proper caching strategies.
File
Anchor to Next stepsNext steps
After applying this recipe, you can:
-
Customize the caching strategy - Adjust cache durations based on your data freshness requirements:
// Short cache for frequently changing datacache: CacheShort()// Long cache for stable datacache: CacheLong()// Custom cache durationcache: CacheCustom({mode: 'must-revalidate',maxAge: 60, // 1 minutestaleWhileRevalidate: 60 * 5, // 5 minutes}) -
Add more third-party APIs - Create additional clients following the same pattern:
- Copy
createRickAndMortyClient.server.tsand adapt for your API - Add the new client to
context.ts - Update TypeScript types in the global declaration
- Copy
-
Implement error handling - Add try-catch blocks and fallback UI:
try {const data = await context.yourApi.query(QUERY);return {data};} catch (error) {console.error('API Error:', error);return {data: null, error: error.message};} -
Monitor performance - Use Oxygen's analytics to track:
- Cache hit rates
- API response times
- Sub-request performance
-
Replace the example - Swap out the Rick & Morty API with your actual third-party service:
- Update the API endpoint in the client
- Modify queries to match your API schema
- Update components to display your data