Code Examples
Ready-to-use code examples for integrating our APIs in your favorite programming languages
Weather API Examples
JavaScript (Fetch API)
// Get current weather
async function getCurrentWeather(location) {
const response = await fetch('https://apistore.store/api/endpoints/weather.php?endpoint=current-weather&location=' + location, {
headers: {
'X-API-Key': 'your_api_key_here'
}
});
const data = await response.json();
return data;
}
// Usage
getCurrentWeather('London')
.then(weather => console.log(weather))
.catch(error => console.error('Error:', error));
Python (requests)
import requests
def get_current_weather(location):
url = 'https://apistore.store/api/endpoints/weather.php'
params = {
'endpoint': 'current-weather',
'location': location
}
headers = {
'X-API-Key': 'your_api_key_here'
}
response = requests.get(url, params=params, headers=headers)
return response.json()
# Usage
weather = get_current_weather('London')
print(weather)
PHP (cURL)
<?php
function getCurrentWeather($location) {
$url = 'https://apistore.store/api/endpoints/weather.php';
$params = http_build_query([
'endpoint' => 'current-weather',
'location' => $location
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-API-Key: your_api_key_here'
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// Usage
$weather = getCurrentWeather('London');
print_r($weather);
?>
cURL Command
curl -X GET "https://apistore.store/api/endpoints/weather.php?endpoint=current-weather&location=London" \
-H "X-API-Key: your_api_key_here"
Finance API Examples
JavaScript - Currency Converter
// Convert currency
async function convertCurrency(from, to, amount) {
const response = await fetch(`https://apistore.store/api/endpoints/finance.php?endpoint=currency-converter&from=${from}&to=${to}&amount=${amount}`, {
headers: {
'X-API-Key': 'your_api_key_here'
}
});
const data = await response.json();
return data;
}
// Usage
convertCurrency('USD', 'ZAR', 100)
.then(result => console.log(`${result.amount} ${result.from} = ${result.converted_amount} ${result.to}`))
.catch(error => console.error('Error:', error));
Python - Stock Prices
import requests
def get_stock_price(symbol):
url = 'https://apistore.store/api/endpoints/finance.php'
params = {
'endpoint': 'stock-price',
'symbol': symbol
}
headers = {
'X-API-Key': 'your_api_key_here'
}
response = requests.get(url, params=params, headers=headers)
return response.json()
# Usage
stock = get_stock_price('AAPL')
print(f"Apple stock price: ${stock['price']}")
AI & ML API Examples
JavaScript - Text Generation
// Generate text using AI
async function generateText(prompt, maxTokens = 100) {
const response = await fetch('https://apistore.store/api/endpoints/ai.php?endpoint=text-generation', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'your_api_key_here'
},
body: JSON.stringify({
prompt: prompt,
max_tokens: maxTokens
})
});
const data = await response.json();
return data;
}
// Usage
generateText('Write a short story about a robot', 150)
.then(result => console.log(result.generated_text))
.catch(error => console.error('Error:', error));
Python - Sentiment Analysis
import requests
def analyze_sentiment(text):
url = 'https://apistore.store/api/endpoints/ai.php?endpoint=sentiment-analysis'
headers = {
'Content-Type': 'application/json',
'X-API-Key': 'your_api_key_here'
}
data = {
'text': text
}
response = requests.post(url, json=data, headers=headers)
return response.json()
# Usage
sentiment = analyze_sentiment("I love this product!")
print(f"Sentiment: {sentiment['sentiment']} (Score: {sentiment['score']})")
Error Handling Examples
JavaScript - Complete Error Handling
async function apiCall(endpoint, options = {}) {
try {
const response = await fetch(endpoint, {
headers: {
'X-API-Key': 'your_api_key_here',
'Content-Type': 'application/json',
...options.headers
},
...options
});
const data = await response.json();
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${data.error || 'Unknown error'}`);
}
if (!data.success) {
throw new Error(data.error || 'API request failed');
}
return data;
} catch (error) {
console.error('API Error:', error.message);
throw error;
}
}
// Usage with error handling
apiCall('https://apistore.store/api/endpoints/weather.php?endpoint=current-weather&location=London')
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Failed:', error.message);
// Handle error appropriately
});