What are MVP Examples?
These are minimal, complete, and runnable code examples that you can copy and use immediately. Each example is self-contained and shows the essential code needed to make API calls to Model Router.Before running these examples, make sure you have:
- Created an API key (see Create API Key)
- Checked which models are available (see List Available Models)
- Replaced
sk-demo123with your actual API key - Replaced the model name with one available to your API key
Python Examples
Using OpenAI Official SDK (Recommended)
Recommended: The OpenAI official SDK is the easiest way to use Model Router. You only need to change the
base_url parameter - everything else works exactly the same as with OpenAI’s API.Installation
Complete Example
Running the Example
- Save the code to a file (e.g.,
chat.py) - Install dependencies:
pip install openai - Replace
api_keyandmodelwith your values - Run:
python chat.py
Key Point: The only difference from using OpenAI’s API directly is setting
base_url="https://app.memorylake.ai/v1". All other code remains exactly the same!Using Environment Variables
You can also use environment variables for better security:Using requests Library (Alternative)
If you prefer using therequests library directly:
Installation
Complete Example
Running the Example
- Save the code to a file (e.g.,
chat.py) - Install dependencies:
pip install requests - Replace
API_KEYandMODELwith your values - Run:
python chat.py
TypeScript/JavaScript Examples
Using OpenAI Official SDK (Recommended)
Recommended: The OpenAI official SDK works seamlessly with Model Router. You only need to change the
baseURL parameter - everything else works exactly the same as with OpenAI’s API.Installation
Complete Example (TypeScript)
Complete Example (JavaScript)
Running the Example
- Save the code to a file (e.g.,
chat.tsorchat.mjs) - Install dependencies:
npm install openai - Replace
apiKeyandmodelwith your values - Run:
- TypeScript:
npx ts-node chat.ts(or compile first withtsc) - JavaScript:
node chat.mjs(ornode chat.jsif using CommonJS)
- TypeScript:
Key Point: The only difference from using OpenAI’s API directly is setting
baseURL: "https://app.memorylake.ai/v1". All other code remains exactly the same!Using Environment Variables
You can also use environment variables for better security:Streaming Examples
Python - Using OpenAI Official SDK
For streaming responses with the OpenAI SDK:Python - Using requests Library
For streaming responses with the requests library:TypeScript/JavaScript - Using OpenAI Official SDK
For streaming responses with the OpenAI SDK:Other Official SDKs
Model Router is compatible with many official SDKs from different AI platforms. Most of them only require configuring theAPI_KEY and BASE_URL to work with Model Router.
Key Point: For most official SDKs, you only need to:
- Set the
baseURLorbase_urltohttps://app.memorylake.ai/v1(or the corresponding endpoint) - Use your Model Router API key as the
apiKeyorapi_key - Everything else works exactly the same as with the original platform’s API
Anthropic Claude SDK
The official Anthropic Claude SDK supports Python, TypeScript/JavaScript, Java, Go, C#, Ruby, and PHP. Example (Python):Vercel AI SDK
The Vercel AI SDK provides a unified interface for multiple AI providers, including OpenAI, Anthropic, Google, and more. Example:Google Gemini SDK
The official Google Gemini SDK supports Python, Node.js, and other languages. Example (Python):General Pattern
Most official SDKs follow a similar pattern:- Install the SDK: Use the official package manager (pip, npm, etc.)
- Configure the client: Set
baseURL/base_urlto Model Router’s endpoint - Set API key: Use your Model Router API key
- Use normally: All other code remains the same
- OpenAI-compatible:
https://app.memorylake.ai/v1 - Claude native:
https://app.memorylake.ai/claude - Gemini native:
https://app.memorylake.ai/gemini
Before using any SDK, make sure to check which models are available to your API key by calling the model list endpoint.
Next Steps
- Test Your Code: Run one of the examples above with your API key
- Check Your Usage: See View Usage and Billing to monitor your API calls
- Handle Errors: Learn about Error Handling if you encounter issues
- Explore More: Check out Direct API Requests for more advanced usage