Skip to main content

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 an ok 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 with unstable_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

  1. Always check the ok property before accessing response data
  2. Use TypeScript for better development experience and error catching
  3. Implement caching for frequently accessed documents
  4. Handle errors gracefully with user-friendly error messages
  5. Use environment variables for configuration
  6. Consider rate limiting if making many API calls
  7. Implement retry logic for network failures
  8. 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
Network timeouts:
  • Implement retry logic with exponential backoff
  • Consider using shorter timeouts for better UX
  • Add loading states to improve perceived performance
Memory issues with large documents:
  • Implement pagination if available
  • Use streaming responses if supported
  • Consider lazy loading for large document collections
TypeScript errors:
  • Ensure you’re using the latest SDK version
  • Check that your TypeScript configuration is compatible
  • Use proper type guards when checking response objects