REST Principles
What is REST?
REST (Representational State Transfer) is an architectural style for APIs.
REST Principles
- Client-Server: Separation of concerns
- Stateless: No client context stored on server
- Cacheable: Responses must define cacheability
- Uniform Interface: Standardized operations
- Layered System: Client can't tell if connected to end server
Magento REST API
Base URL: https://your-store.com/rest/
Default store: /rest/V1/
Specific store: /rest/{store_code}/V1/
Examples:
https://your-store.com/rest/V1/products
https://your-store.com/rest/default/V1/products
https://your-store.com/rest/germany/V1/products
API Versions
# Current API version: V1
# Endpoint format: /rest/{store}/V1/{resource}
# Example endpoints:
GET /rest/V1/products
POST /rest/V1/products
GET /rest/V1/customers
GET /rest/V1/orders
Resources and Endpoints
Common Resources
| Resource | Endpoint |
|---|---|
| Products | /rest/V1/products |
| Categories | /rest/V1/categories |
| Customers | /rest/V1/customers |
| Orders | /rest/V1/orders |
| Carts | /rest/V1/carts |
| Inventory | /rest/V1/inventory/source-items |
Resource Relationships
Products → Categories
Customers → Orders
Orders → Items → Products
Carts → Items → Products
Nested Resources
# Get products in category
GET /rest/V1/categories/{categoryId}/products
# Get customer orders
GET /rest/V1/customers/{customerId}/orders
# Get order items
GET /rest/V1/orders/{orderId}/items
Search Resources
# Search products
GET /rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=name&searchCriteria[filter_groups][0][filters][0][value]=%25shirt%25&searchCriteria[filter_groups][0][filters][0][condition_type]=like
# Search orders
GET /rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=status&searchCriteria[filter_groups][0][filters][0][value]=pending
HTTP Methods
Method Overview
| Method | Purpose | Example |
|---|---|---|
| GET | Read resource | Get product details |
| POST | Create resource | Create new product |
| PUT | Update resource | Update product price |
| DELETE | Delete resource | Delete product |
GET Requests
# Get all products
curl -X GET 'https://store.com/rest/V1/products' \
-H 'Authorization: Bearer {token}'
# Get specific product
curl -X GET 'https://store.com/rest/V1/products/SKU-123' \
-H 'Authorization: Bearer {token}'
# Get with search criteria
curl -X GET 'https://store.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=sku&searchCriteria[filterGroups][0][filters][0][value]=SKU-123' \
-H 'Authorization: Bearer {token}'
POST Requests
# Create product
curl -X POST 'https://store.com/rest/V1/products' \
-H 'Authorization: Bearer {token}' \
-H 'Content-Type: application/json' \
-d '{
"product": {
"sku": "SKU-123",
"name": "Test Product",
"price": 29.99,
"status": 1,
"visibility": 4,
"type_id": "simple"
}
}'
PUT Requests
# Update product
curl -X PUT 'https://store.com/rest/V1/products/SKU-123' \
-H 'Authorization: Bearer {token}' \
-H 'Content-Type: application/json' \
-d '{
"product": {
"price": 39.99
}
}'
DELETE Requests
# Delete product
curl -X DELETE 'https://store.com/rest/V1/products/SKU-123' \
-H 'Authorization: Bearer {token}'
Content Types
Request Content Types
# JSON (most common)
-H 'Content-Type: application/json'
# Form data
-H 'Content-Type: application/x-www-form-urlencoded'
# Multipart (file upload)
-H 'Content-Type: multipart/form-data'
Response Content Types
# JSON response (default)
-H 'Accept: application/json'
# XML response
-H 'Accept: application/xml'
JSON Format
{
"product": {
"id": 1,
"sku": "SKU-123",
"name": "Test Product",
"price": 29.99,
"status": 1,
"visibility": 4,
"type_id": "simple",
"created_at": "2024-01-01 00:00:00",
"updated_at": "2024-01-01 00:00:00"
}
}
Error Response Format
{
"message": "Invalid product data",
"errors": [
{
"message": "The SKU is required.",
"field": "sku",
"code": 0
}
]
}
Pagination Headers
# Response headers
X-Total-Count: 100
X-Page-Size: 20
X-Current-Page: 1
Quiz
1. What is the base URL for Magento REST API?
2. Which HTTP method creates a new resource?
3. What is the default response format?
Flashcards
Question
What is the Magento REST base URL?
Click to reveal answer
Answer
/rest/V1/ or /rest/{store_code}/V1/
Question
Which HTTP method reads resources?
Click to reveal answer
Answer
GET
Question
Which HTTP method creates resources?
Click to reveal answer
Answer
POST
Question
Which HTTP method updates resources?
Click to reveal answer
Answer
PUT
Question
What is the default content type?
Click to reveal answer
Answer
application/json
Revision Notes
Key Takeaways
- 1. REST uses HTTP methods for CRUD operations
- 2. Base URL is /rest/V1/
- 3. JSON is the default content type
- 4. Resources are accessed via endpoints
- 5. Search criteria filter results
Interview Tips
- • Explain REST principles
- • Know the common endpoints
- • Discuss HTTP methods and their uses
- • Be ready to make API calls
Cheat Sheet
Base URL: /rest/V1/
Methods:
GET - Read
POST - Create
PUT - Update
DELETE - Delete
Content-Type: application/json
Accept: application/json
Endpoints:
/products
/customers
/orders
/carts