Creating and Managing Resources 🗄️ Archived
Complete guide to CRUD operations on resources
Learn how to perform complete CRUD (Create, Read, Update, Delete) operations.
Creating a Resource
Basic Creation
curl -X POST https://api.example.com/v1/resources \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "My Resource",
"description": "A sample resource",
"type": "standard",
"tags": ["tutorial", "example"]
}'Response
{
"data": {
"id": "res_abc123",
"name": "My Resource",
"description": "A sample resource",
"type": "standard",
"tags": ["tutorial", "example"],
"status": "active",
"created_at": "2025-02-12T10:00:00Z",
"updated_at": "2025-02-12T10:00:00Z"
}
}Reading Resources
Get Single Resource
curl -X GET https://api.example.com/v1/resources/res_abc123 \
-H "Authorization: Bearer YOUR_TOKEN"List All Resources
curl -X GET https://api.example.com/v1/resources \
-H "Authorization: Bearer YOUR_TOKEN"Filter Resources
curl -X GET "https://api.example.com/v1/resources?filter[type]=standard&filter[status]=active" \
-H "Authorization: Bearer YOUR_TOKEN"Updating Resources
Full Update (PUT)
Replace the entire resource:
curl -X PUT https://api.example.com/v1/resources/res_abc123 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Resource",
"description": "Updated description",
"type": "premium",
"tags": ["updated", "premium"]
}'Partial Update (PATCH)
Update only specific fields:
curl -X PATCH https://api.example.com/v1/resources/res_abc123 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"description": "New description only"
}'Deleting Resources
Soft Delete
Mark as deleted but retain in database:
curl -X DELETE https://api.example.com/v1/resources/res_abc123 \
-H "Authorization: Bearer YOUR_TOKEN"Hard Delete
Permanently remove (if supported):
curl -X DELETE https://api.example.com/v1/resources/res_abc123?hard=true \
-H "Authorization: Bearer YOUR_TOKEN"Bulk Operations
Create Multiple Resources
curl -X POST https://api.example.com/v1/resources/bulk \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"resources": [
{"name": "Resource 1", "type": "standard"},
{"name": "Resource 2", "type": "premium"},
{"name": "Resource 3", "type": "standard"}
]
}'Update Multiple Resources
curl -X PATCH https://api.example.com/v1/resources/bulk \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"ids": ["res_1", "res_2", "res_3"],
"updates": {
"status": "archived"
}
}'Best Practices
- Validation: Always validate input before sending
- Idempotency: Use idempotency keys for create operations
- Optimistic Locking: Use ETags for update operations
- Soft Deletes: Prefer soft deletes for data recovery
- Batch Operations: Use bulk endpoints when working with multiple resources
Example: Complete Python Script
import requests
class ResourceManager:
def __init__(self, base_url, token):
self.base_url = base_url
self.headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
def create(self, data):
response = requests.post(
f"{self.base_url}/resources",
json=data,
headers=self.headers
)
return response.json()
def get(self, resource_id):
response = requests.get(
f"{self.base_url}/resources/{resource_id}",
headers=self.headers
)
return response.json()
def update(self, resource_id, data):
response = requests.patch(
f"{self.base_url}/resources/{resource_id}",
json=data,
headers=self.headers
)
return response.json()
def delete(self, resource_id):
response = requests.delete(
f"{self.base_url}/resources/{resource_id}",
headers=self.headers
)
return response.status_code == 204
# Usage
manager = ResourceManager("https://api.example.com/v1", "YOUR_TOKEN")
# Create
resource = manager.create({
"name": "Test Resource",
"type": "standard"
})
# Read
retrieved = manager.get(resource["data"]["id"])
# Update
updated = manager.update(resource["data"]["id"], {
"name": "Updated Resource"
})
# Delete
deleted = manager.delete(resource["data"]["id"])