Interacting with the API

Did you Get a Magento 2 REST API Token With MyMagento? Then let’s start using the API!

Search Results: The Model Classes

The result of any SearchQuery will be parsed and wrapped by a Model class in the magento.models subpackage.

These classes make the API response data easier to work with.

They also provide endpoint-specific methods to update store data and search for related items.

Example: Retrieving every Order containing a Product

Let’s retrieve a Product using by_sku()

>>> product = api.products.by_sku("24-MB01")

We can search for orders containing this product in multiple ways:

# Using the Product itself
>>> product.get_orders()

[<Magento Order: "#000000003" placed on 2022-12-21 08:09:33>, ... ]

# Using an OrderSearch
>>> api.orders.by_product(product)
>>> api.orders.by_product_id(
>>> api.orders.by_sku(product.sku)

[<Magento Order: "#000000003" placed on 2022-12-21 08:09:33>, ... ]

Example: Retrieving all Products and Invoices for a Category

>>> category = api.categories.by_name("Watches")
>>> category.get_products()
>>> category.get_invoices()

[<Magento Product: 24-MG04>, <Magento Product: 24-MG01>, <Magento Product: 24-MG03>, ... ]
[<Magento Invoice: "#000000004"> for <Magento Order: "#000000004" placed on 2022-11-14 03:27:33>, ... ]

Example: Updating the Thumbnail MediaEntry of a Product

 # Update product thumbnail label on specific store view
>>> product.thumbnail.set_alt_text("bonjour", scope="FR")
>>> print(product.thumbnail)

 <MediaEntry 3417 for <Magento Product: 24-MB01>: bonjour>

Tip: Set the Store Scope

If you have multiple store views, a store_code can be specified when retrieving/updating data

Building Custom Search Queries

In addition to the predefined methods, you can also build your own queries

Example: Retrieve Orders Over $50 Placed Since the Start of 2023

>>> api.orders.add_criteria(
...    field="grand_total",
...    value="50",
...    condition="gt"
... ).since("2023-01-01").execute()

[<Magento Order: "#000000012" placed on 2023-01-02 05:19:55>, ...]

Making Authorized Requests

The Client can be used to generate the url_for() any API endpoint, including a store scope.

You can use this URL to make an authorized get(), post(), put(), or delete() request.

Example: Making a get() Request

# Request the data for credit memo with id 7
>>> url = api.url_for('creditmemo/7')
>>> response = api.get(url)
>>> print(response.json())

{'adjustment': 1.5, 'adjustment_negative': 0, 'adjustment_positive': 1.5, 'base_adjustment': 1.5,  ... }


A search() is simpler than making get() requests, as the result will be wrapped by APIResponse or other Model

# Retrieve credit memo with id 7 using a search
>>> memo ="creditmemo").by_id(7)
>>> print(
>>> print(memo)

{'adjustment': 1.5, 'adjustment_negative': 0, 'adjustment_positive': 1.5, 'base_adjustment': 1.5,  ... }
<magento.models.model.APIResponse object at 0x000001BA42FD0FD1>

Example: Making a post() Request

# Add a comment to credit memo with id 7
>>> url = api.url_for("creditmemo/7/comments")
>>> payload = {
        "entity": {
            "comment": "this is a comment",
            "is_customer_notified": 0,
            "is_visible_on_front": 0,
            "parent_id": 20
>>> response =, payload)


The Model.data_endpoint() will usually be close to the url to post() to

# The same as above, but using a search
>>> memo ="creditmemo").by_id(7)
>>> url = memo.data_endpoint() + '/comments'
>>> response =, payload)