Advanced Usage
Learn advanced patterns, error handling, and optimization techniques for The Prompting Company TypeScript SDK.Error Handling
The SDK returns a structured response with anok property to indicate success or failure. Always check this property before accessing data:
Client Configuration
Environment-Based Configuration
Create different configurations for different environments:Multiple Client Instances
If you need to access multiple products or organizations:Caching Strategies
Simple In-Memory Cache
Implement caching to reduce API calls:Next.js App Router with Caching
Use Next.js’s built-in caching withunstable_cache:
Batch Operations
If you need to fetch multiple documents efficiently:React Hooks
Create custom hooks for common patterns:TypeScript Utilities
Create utility types for better type safety:Development and Debugging
Debug Mode
Add debug logging during development:Performance Optimization
Parallel Document Loading
Load multiple documents in parallel for better performance:Best Practices
- Always check the
okproperty before accessing response data - Use TypeScript for better development experience and error catching
- Implement caching for frequently accessed documents
- Handle errors gracefully with user-friendly error messages
- Use environment variables for configuration
- Consider rate limiting if making many API calls
- Implement retry logic for network failures
- Use batch operations when possible to reduce API calls
Troubleshooting
Common Issues
Document not found errors:- Verify the document path exists in your organization
- Check that your product slug is correct
- Ensure your API key has access to the document
- Implement retry logic with exponential backoff
- Consider using shorter timeouts for better UX
- Add loading states to improve perceived performance
- Implement pagination if available
- Use streaming responses if supported
- Consider lazy loading for large document collections
- Ensure you’re using the latest SDK version
- Check that your TypeScript configuration is compatible
- Use proper type guards when checking response objects