Skip to content
intermediate Phase 60 · REST Implementation

REST CRUD Operations

Implementing REST CRUD operations in Magento 2: GET/POST/PUT/DELETE for products, customers, and orders

45m
0 problems
Topic Progress 0%

Product CRUD

Create Product

curl -X POST 'https://store.com/rest/V1/products' \
     -H 'Authorization: Bearer admin_token' \
     -H 'Content-Type: application/json' \
     -d '{
         "product": {
             "sku": "SKU-123",
             "name": "Test Product",
             "price": 29.99,
             "status": 1,
             "visibility": 4,
             "type_id": "simple",
             "attribute_set_id": 4,
             "extension_attributes": {
                 "stock_item": {
                     "qty": 100,
                     "is_in_stock": true
                 }
             }
         }
     }'

Read Product

# Get by SKU
curl -X GET 'https://store.com/rest/V1/products/SKU-123' \
     -H 'Authorization: Bearer admin_token'

# Get with search criteria
curl -X GET 'https://store.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=name&searchCriteria[filterGroups][0][filters][0][value]=%25Test%25&searchCriteria[filterGroups][0][filters][0][conditionType]=like' \
     -H 'Authorization: Bearer admin_token'

Update Product

curl -X PUT 'https://store.com/rest/V1/products/SKU-123' \
     -H 'Authorization: Bearer admin_token' \
     -H 'Content-Type: application/json' \
     -d '{
         "product": {
             "price": 39.99,
             "name": "Updated Product Name"
         }
     }'

Delete Product

curl -X DELETE 'https://store.com/rest/V1/products/SKU-123' \
     -H 'Authorization: Bearer admin_token'

Customer CRUD

Create Customer

curl -X POST 'https://store.com/rest/V1/customers' \
     -H 'Content-Type: application/json' \
     -d '{
         "customer": {
             "email": "customer@example.com",
             "firstname": "John",
             "lastname": "Doe",
             "addresses": [
                 {
                     "defaultShipping": true,
                     "defaultBilling": true,
                     "firstname": "John",
                     "lastname": "Doe",
                     "region": {
                         "regionCode": "NY",
                         "region": "New York"
                     },
                     "postcode": "10001",
                     "countryId": "US",
                     "street": ["123 Main St"],
                     "telephone": "555-1234"
                 }
             ]
         }
     }'

Read Customer

# Get customer ID
curl -X GET 'https://store.com/rest/V1/customers/search?searchCriteria[filterGroups][0][filters][0][field]=email&searchCriteria[filterGroups][0][filters][0][value]=customer@example.com' \
     -H 'Authorization: Bearer admin_token'

# Get customer by ID
curl -X GET 'https://store.com/rest/V1/customers/1' \
     -H 'Authorization: Bearer customer_token'

# Get current customer
curl -X GET 'https://store.com/rest/V1/customers/me' \
     -H 'Authorization: Bearer customer_token'

Update Customer

curl -X PUT 'https://store.com/rest/V1/customers/1' \
     -H 'Authorization: Bearer admin_token' \
     -H 'Content-Type: application/json' \
     -d '{
         "customer": {
             "email": "newemail@example.com",
             "firstname": "John",
             "lastname": "Doe"
         }
     }'

Delete Customer

curl -X DELETE 'https://store.com/rest/V1/customers/1' \
     -H 'Authorization: Bearer admin_token'

Order Operations

List Orders

curl -X GET 'https://store.com/rest/V1/orders?searchCriteria[filterGroups][0][filters][0][field]=status&searchCriteria[filterGroups][0][filters][0][value]=pending' \
     -H 'Authorization: Bearer admin_token'

Get Order Details

# By order ID
curl -X GET 'https://store.com/rest/V1/orders/1' \
     -H 'Authorization: Bearer admin_token'

# By order number
curl -X GET 'https://store.com/rest/V1/orders?searchCriteria[filterGroups][0][filters][0][field]=increment_id&searchCriteria[filterGroups][0][filters][0][value]=100000001' \
     -H 'Authorization: Bearer admin_token'

Order Items

# Get order items
curl -X GET 'https://store.com/rest/V1/orders/1/items' \
     -H 'Authorization: Bearer admin_token'

Invoice Order

curl -X POST 'https://store.com/rest/V1/order/1/invoice' \
     -H 'Authorization: Bearer admin_token' \
     -H 'Content-Type: application/json' \
     -d '{
         "capture": true,
         "itemsQty": [
             {
                 "order_item_id": 1,
                 "qty": 1
             }
         ]
     }'

Ship Order

curl -X POST 'https://store.com/rest/V1/order/1/ship' \
     -H 'Authorization: Bearer admin_token' \
     -H 'Content-Type: application/json' \
     -d '{
         "itemsQty": [
             {
                 "order_item_id": 1,
                 "qty": 1
             }
         ],
         "tracks": [
             {
                 "carrier_code": "custom",
                 "title": "Custom Carrier",
                 "track_number": "TRACK123"
             }
         ]
     }'

Cancel Order

curl -X POST 'https://store.com/rest/V1/orders/1/cancel' \
     -H 'Authorization: Bearer admin_token'

Search Criteria

Basic Search

# Simple filter
curl 'https://store.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=sku&searchCriteria[filterGroups][0][filters][0][value]=SKU-123'

# Like filter
curl 'https://store.com/rest/V1/products?searchCriteria[filterGroups][0][filters][0][field]=name&searchCriteria[filterGroups][0][filters][0][value]=%25shirt%25&searchCriteria[filterGroups][0][filters][0][conditionType]=like'

Search Criteria Structure

searchCriteria:
  filterGroups:
    - filters:
        - field: sku
          value: SKU-123
          conditionType: eq

Condition Types

Condition Description
eq Equals (default)
neq Not equals
like Contains
nlike Not contains
in In array
nin Not in array
gt Greater than
gteq Greater than or equal
lt Less than
lteq Less than or equal

Pagination

# Page and size
curl 'https://store.com/rest/V1/products?searchCriteria[pageSize]=10&searchCriteria[currentPage]=1'

Sorting

# Sort by field
curl 'https://store.com/rest/V1/products?searchCriteria[sortOrders][0][field]=price&searchCriteria[sortOrders][0][direction]=ASC'

Quiz

1. Which HTTP method creates a product?

Question 1 options

2. How do you filter products by name?

Question 2 options

3. How do you paginate results?

Question 3 options

Flashcards

Question

How do you create a product?

Answer

POST /V1/products with product data

Question

How do you get a product by SKU?

Answer

GET /V1/products/{sku}

Question

How do you filter results?

Answer

Use searchCriteria with filterGroups

Question

How do you paginate?

Answer

searchCriteria[pageSize] and searchCriteria[currentPage]

Question

How do you sort results?

Answer

searchCriteria[sortOrders][0][field] and direction

Revision Notes

Key Takeaways

  • 1. POST creates, GET reads, PUT updates, DELETE removes
  • 2. Products use SKU as identifier
  • 3. Customers can be accessed via /customers/me
  • 4. Orders support invoice, ship, and cancel operations
  • 5. Search criteria filter, sort, and paginate results

Interview Tips

  • Know the CRUD endpoints for each resource
  • Understand search criteria structure
  • Be ready to make API calls for common operations
  • Discuss pagination and sorting approaches

Cheat Sheet

Products:
  POST /V1/products - Create
  GET /V1/products/{sku} - Read
  PUT /V1/products/{sku} - Update
  DELETE /V1/products/{sku} - Delete

Customers:
  POST /V1/customers - Create
  GET /V1/customers/{id} - Read
  GET /V1/customers/me - Current customer

Orders:
  GET /V1/orders - List
  POST /V1/order/{id}/invoice - Invoice
  POST /V1/order/{id}/ship - Ship