JavaScript
JavaScript Examples
Use QuietStack with JavaScript - no installation required, just HTTP requests
Basic Fetch Example
Simple example using the modern fetch API
// Basic chat completion with QuietStack
const response = await fetch('https://api.quietstack.ai/v1/proxy', {
method: 'POST',
headers: {
'Authorization': 'Bearer qs_live_YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
provider: 'openai',
model: 'gpt-3.5-turbo',
messages: [{
role: 'user',
content: 'What is the capital of France?'
}]
})
});
const data = await response.json();
console.log('AI Response:', data.response);
console.log('Blockchain Hash:', data.verification.hash);
Node.js Example
Server-side JavaScript with built-in https module
// Node.js example with error handling
const https = require('https');
async function callQuietStack(prompt) {
const postData = JSON.stringify({
provider: 'openai',
model: 'gpt-4',
messages: [{
role: 'user',
content: prompt
}]
});
const options = {
hostname: 'api.quietstack.ai',
port: 443,
path: '/v1/proxy',
method: 'POST',
headers: {
'Authorization': 'Bearer qs_live_YOUR_API_KEY',
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const result = JSON.parse(data);
resolve(result);
} catch (error) {
reject(error);
}
});
});
req.on('error', (error) => {
reject(error);
});
req.write(postData);
req.end();
});
}
// Usage
callQuietStack('Explain quantum computing')
.then(result => {
console.log('Response:', result.response);
console.log('Verification:', result.verification.blockchain_tx);
})
.catch(error => console.error('Error:', error));
React Component
Interactive React component with blockchain verification
// React component example
import { useState } from 'react';
function AIChat() {
const [message, setMessage] = useState('');
const [response, setResponse] = useState('');
const [loading, setLoading] = useState(false);
const [verification, setVerification] = useState(null);
const sendMessage = async () => {
if (!message.trim()) return;
setLoading(true);
try {
const res = await fetch('https://api.quietstack.ai/v1/proxy', {
method: 'POST',
headers: {
'Authorization': 'Bearer qs_live_YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
provider: 'openai',
model: 'gpt-3.5-turbo',
messages: [{
role: 'user',
content: message
}]
})
});
const data = await res.json();
setResponse(data.response);
setVerification(data.verification);
} catch (error) {
console.error('Error:', error);
} finally {
setLoading(false);
}
};
return (
<div>
<input
value={message}
onChange={(e) => setMessage(e.target.value)}
placeholder="Ask something..."
disabled={loading}
/>
<button onClick={sendMessage} disabled={loading}>
{loading ? 'Thinking...' : 'Send'}
</button>
{response && (
<div>
<h3>AI Response:</h3>
<p>{response}</p>
{verification && (
<div>
<p>✅ Blockchain Verified</p>
<a href={verification.blockchain_tx} target="_blank">
View Transaction
</a>
</div>
)}
</div>
)}
</div>
);
}
export default AIChat;
Next.js API Route
Server-side API route with environment variable for API key
// Next.js API route example
// pages/api/chat.js or app/api/chat/route.js
export async function POST(request) {
try {
const { message } = await request.json();
const response = await fetch('https://api.quietstack.ai/v1/proxy', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.QUIETSTACK_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
provider: 'openai',
model: 'gpt-3.5-turbo',
messages: [{
role: 'user',
content: message
}]
})
});
const data = await response.json();
// Log the blockchain verification
console.log('Request verified on blockchain:', data.verification.hash);
return Response.json({
response: data.response,
verified: true,
blockchainHash: data.verification.hash
});
} catch (error) {
console.error('Error:', error);
return Response.json({ error: 'Failed to process request' }, { status: 500 });
}
}
Environment Setup
Keep your API key secure using environment variables
1. Create .env file:
QUIETSTACK_API_KEY=qs_live_your_actual_api_key_here
2. Use in your code:
const apiKey = process.env.QUIETSTACK_API_KEY;
Ready to try it out?
Get your API key and test these examples in our playground