API Integration Examples
This section provides practical examples and code snippets to help you integrate with our API seamlessly.
Example 1: Fetching User Data (GET Request)
This example demonstrates how to retrieve user information by making a GET request to the /users/{id} endpoint.
Endpoint
- Method: GET
- URL:
https://api.awesomeapi.com/v1/users/{id}
Request Headers
- Authorization:
Bearer YOUR_API_KEY - Content-Type:
application/json
URL Parameters
- id (required): The unique identifier of the user.
Example Implementation (JavaScript using Fetch API):
async function getUser(userId, apiKey) {
const url = `https://api.awesomeapi.com/v1/users/${userId}`;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(`HTTP error ${response.status}: ${errorData.message || 'Unknown error'}`);
}
const userData = await response.json();
console.log('User Data:', userData);
return userData;
} catch (error) {
console.error('Error fetching user:', error);
// Handle error appropriately in your application
}
}
// Usage:
// const apiKey = 'YOUR_ACTUAL_API_KEY';
// getUser('user_123abc', apiKey);
Try this Example
Example 2: Creating a New Resource (POST Request)
This example illustrates how to create a new resource, such as a product, by sending a POST request to the /products endpoint.
Endpoint
- Method: POST
- URL:
https://api.awesomeapi.com/v1/products
Request Headers
- Authorization:
Bearer YOUR_API_KEY - Content-Type:
application/json
Request Body (JSON)
- name (required): Name of the product.
- description: Description of the product.
- price (required): Price of the product.
Example Implementation (Python using Requests library):
import requests
import json
def create_product(api_key, product_data):
url = "https://api.awesomeapi.com/v1/products"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
try:
response = requests.post(url, headers=headers, data=json.dumps(product_data))
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
created_product = response.json()
print("Product Created:", created_product)
return created_product
except requests.exceptions.RequestException as e:
print(f"Error creating product: {e}")
# Handle error appropriately in your application
return None
# Usage:
# api_key = "YOUR_ACTUAL_API_KEY"
# new_product = {
# "name": "Super Gadget",
# "description": "The latest and greatest gadget.",
# "price": 99.99
# }
# create_product(api_key, new_product)
Try this Example
Example 3: Updating an Existing Resource (PUT Request)
Learn how to update an existing resource by making a PUT request to the /items/{id} endpoint.
Endpoint
- Method: PUT
- URL:
https://api.awesomeapi.com/v1/items/{id}
Request Headers
- Authorization:
Bearer YOUR_API_KEY - Content-Type:
application/json
URL Parameters & Request Body
- id (required): Unique identifier of the item to update.
- Request Body (JSON): Fields to update (e.g.,
status,quantity).
Example Implementation (cURL):
curl -X PUT \
https://api.awesomeapi.com/v1/items/item_xyz789 \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"status": "completed",
"quantity": 15
}'
Try this Example
Example 4: Deleting a Resource (DELETE Request)
This example shows how to remove a resource using a DELETE request to the /tasks/{id} endpoint.
Endpoint
- Method: DELETE
- URL:
https://api.awesomeapi.com.com/v1/tasks/{id}
Request Headers
- Authorization:
Bearer YOUR_API_KEY
URL Parameters
- id (required): The unique identifier of the task to delete.
Example Implementation (Node.js using Axios):
const axios = require('axios');
async function deleteTask(taskId, apiKey) {
const url = `https://api.awesomeapi.com/v1/tasks/${taskId}`;
try {
const response = await axios.delete(url, {
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
if (response.status === 204) { // No Content for successful deletion
console.log(`Task ${taskId} deleted successfully.`);
} else {
console.warn(`Received status code ${response.status} for deletion.`);
}
return true;
} catch (error) {
console.error(`Error deleting task ${taskId}:`, error.response ? error.response.data.message : error.message);
// Handle error appropriately
return false;
}
}
// Usage:
// const apiKey = 'YOUR_ACTUAL_API_KEY';
// deleteTask('task_987zyx', apiKey);
Try this Example
Need more help? Check out our full API documentation or contact our support team.