Skip to main content
Logo for MyMagento: Python Magento 2 REST API Wrapper

A Python package that wraps and extends the Magento 2 REST API

Explore the docs »


PyPI Version GitHub Repository https://static.pepy.tech/personalized-badge/my-magento?period=total&units=none&left_color=grey&right_color=blue&left_text=Downloads Documentation Status

About MyMagento

📝 What’s MyMagento?

MyMagento is a highly interconnected package that wraps and extends the Magento 2 REST API, providing a more intuitive and user-friendly interface to access and update your store.

MyMagento simplifies interaction with the Magento 2 REST API

If you’ve worked with the Magento 2 API, you’ll know that not all endpoints are created equally.

MyMagento aims to streamline your workflow by simplifying a variety of commonly needed API operations.

Main Components

💻 The Client

  • Handles all API interactions

  • Supports multiple store views

  • Provides access to all other package components

🔍 The SearchQuery and Subclasses

🧠 The Model Subclasses

  • Wrap all API responses in the package

  • Provide additional endpoint-specific methods to retrieve and update data

Available Endpoints

MyMagento is compatible with every API endpoint

Endpoints are wrapped with a Model and SearchQuery subclass as follows:

Endpoint

Client Shortcut

SearchQuery Subclass

Model Subclass

orders

Client.orders

OrderSearch

Order

orders/items

Client.order_items

OrderItemSearch

OrderItem

invoices

Client.invoices

InvoiceSearch

Invoice

products

Client.products

ProductSearch

Product

products/attributes

Client.product_attributes

ProductAttributeSearch

ProductAttribute

categories

Client.categories

CategorySearch

Category

customers

Client.customers

CustomerSearch

Customer

endpoint

Client.search('endpoint')

SearchQuery

APIResponse

⚙ Installing MyMagento

To install using pip:

pip install my-magento

Please note that MyMagento requires Python >= 3.10

📚 Documentation

Full documentation can be found on ReadTheDocs


QuickStart: Login with MyMagento

MyMagento uses the Client class to handle all interactions with the API.

💡 Tip

See Get a Magento 2 REST API Token With MyMagento for full details on generating an access token

Setting the Login Credentials

To generate an ACCESS_TOKEN you’ll need to authenticate() your USER_CREDENTIALS.

Creating a Client requires a domain, username, and password at minimum.

>>> domain = 'website.com'
>>> username ='username'
>>> password = 'password'

If you’re using a local installation of Magento you’ll need to set local=True. Your domain should look like this:

>>> domain = '127.0.0.1/path/to/magento'

Getting a Client

Option 1: Initialize a Client Directly

from magento import Client

>>> api = Client(domain, username, password, **kwargs)

Option 2: Call get_api()

import magento

>>> api = magento.get_api(**kwargs)

get_api() takes the same keyword arguments as the Client

  • If the domain, username, or password are missing, it will attempt to use the following environment variables:

import os

os.environ['MAGENTO_DOMAIN'] = domain
os.environ['MAGENTO_USERNAME']= username
os.environ['MAGENTO_PASSWORD']= password

Getting an ACCESS_TOKEN

Unless you specify login=False, the Client will automatically call authenticate() once initialized:

>> api.authenticate()

|[ MyMagento | website_username ]|:  Authenticating username on website.com...
|[ MyMagento | website_username ]|:  Logged in to username

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(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>, ...]

📋 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,  ... }

📝 Note

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 = api.search("creditmemo").by_id(7)
>>> print(memo.data)
>>> print(memo)

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

my-magento-2.2.0.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

my_magento-2.2.0-py3-none-any.whl (42.5 kB view details)

Uploaded Python 3

File details

Details for the file my-magento-2.2.0.tar.gz.

File metadata

  • Download URL: my-magento-2.2.0.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.1

File hashes

Hashes for my-magento-2.2.0.tar.gz
Algorithm Hash digest
SHA256 0629c41c6117ef7f72741a925ae1cc9bb6356939a28efe036a9f1a4ee3aec103
MD5 836fa1a99b5c69eae2d6e74974ccc8ca
BLAKE2b-256 8ca026f55a697790174454e70ff262b698bec2797eee896e7babc7cc965660b1

See more details on using hashes here.

File details

Details for the file my_magento-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: my_magento-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 42.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.1

File hashes

Hashes for my_magento-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c037f152ea055738269fedc363508063d10affed4c89afd52e73e6074a69f50a
MD5 d5a1c507421f3fbb7936dcc637df0ab0
BLAKE2b-256 ea599e177696838e178087c1f50188faa14e2de82a1c1790e082eda3d8b85b1d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 files

2.1.4

2 files

2.1.3

2 files

2.1.2

2 files

2.1.1

3 files

2.1.0

3 files

2.0.1

3 files

2.0.0

3 files

1.1.2

2 files

1.1.1

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page