Building an application with Axios, or any other REST API client?
Using Requesty with Axios is straightforward - you just need to point your HTTP requests to the Requesty router endpoint.
This approach gives you maximum flexibility while still accessing all of Requesty’s powerful features.
This simple integration unlocks powerful features, such as:
All of this is available while maintaining full control over your HTTP requests.
With Requesty, you can access over 250+ models from various providers. To specify a model, you must include the provider prefix, like openai/gpt-4o-mini
or anthropic/claude-sonnet-4-20250514
.
You can find the full list of available models in the Model Library.
Basic Usage
Here’s how to make a simple chat completion request using Axios:
import axios from 'axios';
// Safely load your API key from environment variables
const REQUESTY_API_KEY = process.env.REQUESTY_API_KEY;
async function chatCompletion() {
try {
const response = await axios.post('https://router.requesty.ai/v1/chat/completions', {
model: "openai/gpt-4o",
messages: [
{ role: "user", content: "Hello, world!" }
]
}, {
headers: {
'Authorization': `Bearer ${REQUESTY_API_KEY}`,
'Content-Type': 'application/json'
}
});
console.log(response.data.choices[0].message.content);
} catch (error) {
console.error('Error:', error.response?.data || error.message);
}
}
chatCompletion();
Streaming Responses
For streaming responses, you can use Server-Sent Events:
import axios from 'axios';
async function streamingChat() {
try {
const response = await axios.post('https://router.requesty.ai/v1/chat/completions', {
model: "openai/gpt-4o",
messages: [
{ role: "user", content: "Write a short story about AI" }
],
stream: true
}, {
headers: {
'Authorization': `Bearer ${process.env.REQUESTY_API_KEY}`,
'Content-Type': 'application/json'
},
responseType: 'stream'
});
response.data.on('data', (chunk) => {
const lines = chunk.toString().split('\n');
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine || !trimmedLine.startsWith('data:')) continue;
const data = trimmedLine.substring(5).trim();
if (data === '[DONE]') {
console.log('\nStream completed');
return;
}
try {
const parsed = JSON.parse(data);
const content = parsed.choices?.[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
} catch (e) {
// Skip invalid JSON lines
}
}
});
} catch (error) {
console.error('Error:', error.response?.data || error.message);
}
}
streamingChat();