MyMagento: Magento 2 REST API wrapper

MyMagento🛒

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 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

endpoint

Client.search("endpoint")

SearchQuery

APIResponse

Installation

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, but 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