MyMagento - Python Magento 2 REST API Wrapper
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
execute()a search query on any endpointIntuitive interface for Building Custom Search Queries
All predefined methods retrieve data using only 1-2 API requests
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 |
|
|
|---|---|---|---|
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|
…
⚙ Installing MyMagento
To install using pip:
pip install my-magento
Please note that MyMagento requires Python >= 3.10
…
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, orpasswordare 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
