Custom API Integration
Interface X can integrate with any search API by creating a custom adapter. This guide walks you through building an adapter from scratch.Overview
Building a custom adapter involves:- Defining your API and app data models
- Creating request and response mappers
- Building endpoint adapters
- Combining them into a complete adapter
Step-by-Step Guide
Step 1: Define Your Types
Define the types for both your API and your application:Step 2: Create Mappers
Create functions to transform between API and app formats.Using Mapper Functions
Using Schema Mappers
For simpler mappings, use schema mappers:/home/daytona/workspace/source/packages/x-adapter/README.md:234
Step 3: Create Endpoint Adapters
Use theendpointAdapterFactory to create endpoint adapters:
/home/daytona/workspace/source/packages/x-adapter/README.md:108
Step 4: Create Additional Endpoints
Create adapters for other endpoints you need:Step 5: Combine into an Adapter
Create a main adapter object that exports all endpoint adapters:Step 6: Use Your Adapter
Use the adapter in your application:Advanced Patterns
Dynamic Endpoints
Use dynamic endpoints for multi-tenant or language-specific APIs:/home/daytona/workspace/source/packages/x-adapter/README.md:162
Custom HTTP Client
Use a custom HTTP client for authentication or custom logic:Reusable Schemas
Create reusable schemas for common data structures:/home/daytona/workspace/source/packages/x-adapter/README.md:410
Error Handling
Add error handling to your adapters:Request Transformation Context
Access the endpoint and other context in mappers:Complete Example
Here’s a complete example adapter for a fictional commerce API:Testing Your Adapter
Create tests for your adapter:Best Practices
Use TypeScript for type safety
Use TypeScript for type safety
Define strict types for your API requests and responses. This catches errors at compile time and provides better autocomplete.
Reuse schemas across endpoints
Reuse schemas across endpoints
If multiple endpoints return similar data (e.g., products), create a shared schema and reuse it with
$extends or $subSchema.Handle errors gracefully
Handle errors gracefully
Add error handling in your response mappers to handle API-level errors and provide meaningful error messages.
Test with real API responses
Test with real API responses
Use actual API responses in your tests. Consider recording responses with tools like MSW or nock.
Document your adapter
Document your adapter
Document which API endpoints are supported, required credentials, and any limitations.
Next Steps
Adapter System
Learn more about the adapter architecture
Platform Adapter
See the Empathy Platform adapter implementation
