Simple examples for integrating VKRA into your application
This guide shows you how to implement the most common VKRA patterns in your application.
The most basic use case is fetching products based on a natural language query.
async function getRecommendations(userIntent) {
const response = await fetch('https://api.vkra.org/search', {
method: 'POST',
headers: {
'x-api-key': process.env.ADTOKENS_API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: userIntent,
limit: 2
})
});
if (!response.ok) {
throw new Error('Failed to fetch recommendations');
}
return await response.json();
}When rendering product results, ensure you include attribution tracking and the required disclosure text.
import React from 'react';
interface Product {
impression_id: string;
title: string;
price: string;
url: string;
image_url: string;
merchant: string;
disclosure_text: string;
}
const ProductCard = ({ product }: { product: Product }) => {
const handleTrackClick = async () => {
// Fire-and-forget click tracking
fetch(`https://api.vkra.org/clicks/${product.impression_id}`, {
method: 'POST',
headers: { 'x-api-key': 'YOUR_API_KEY' }
}).catch(err => console.error('Tracking failed', err));
};
return (
<div className="product-card">
<img src={product.image_url} alt={product.title} />
<h3>{product.title}</h3>
<p className="price">{product.price}</p>
<p className="merchant">Sold by {product.merchant}</p>
<a
href={product.url}
onClick={handleTrackClick}
target="_blank"
rel="noopener noreferrer"
>
Buy Now
</a>
{product.disclosure_text && (
<p className="disclosure">{product.disclosure_text}</p>
)}
</div>
);
};Always wrap your API calls in error handling logic to ensure your agent can fall back gracefully if the service is unavailable.
import requests
def fetch_ads(query):
try:
response = requests.post(
"https://api.vkra.org/search",
headers={"x-api-key": "YOUR_KEY"},
json={"query": query},
timeout=2.0 # Keep it fast!
)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"VKRA error: {e}")
return {"results": []} # Graceful fallback