PyMercadoPago is user-friendly library to interact with the MercadoPago API. Python 3 only.
To install it from PyPI, simply run:
pip install pymercadopago
If you haven't done so already, obtain your
Create a new
mercadopago.Client instance and pass it your credentials:
import mercadopago CLIENT_ID = 'XXX' CLIENT_SECRET = 'XXX' mp = mercadopago.Client(CLIENT_ID, CLIENT_SECRET)
You can navigate the full API from the client methods. Try running the above code in a Python shell and explore them.
# Get the invoice with ID 1234 mp.invoices.get('1234')
# Get the current user account balance mp.users.account_balance()
# Create a new customer instance mp.customers.create( first_name='Federico' last_name='Bond', # ... )
In general, assuming
mp is a
mercadopago.Client instance and there is
an endpoint documented at (for example)
you can do:
- List all customers. Pass pagination parameters via keyword arguments.
- Create a new Customer.
- Delete a Customer.
- Update a Customer, include
ìdin your keyword arguments.
- Search for customers matching params.
Not all methods are available for all resources, and some additional
convenience methods have been added to some. To learn more, check out the
official docs and/or the code from the
Nested resources like are usually accessed by following the corresponding resource paths. For example:
# GET /v1/customers/:id/cards # ---- mp.customers.cards(id).list()
All methods return a
Response object if successful (HTTP status code in the
2XX range) or raise an instance of
Every API call you make will return a
Response instance with the following
||The HTTP status_code returned by the API.|
||The decoded JSON response returned by the API.|
||The requested URL.|
If MercadoPago returns a response with pagination information, a
PaginatedResponse will be returned instead. Paginated responses have the
following additional methods:
- Total amount of records in this collection.
- Maximum number of records for this page.
- Number of records skipped to reach this page.
- List of records in this request. This is different from
.datawhich contains the full body of the response, with the pagination info.
- Whether there are any preceding pages.
- Whether there are any following pages.
- Requests the previous page and returns a
- Requests the next page and returns a
- Returns a generator of records that will automatically request new pages when necessary.
If there is a connection error or the HTTP response contains a non-2XX status
code, the method will raise an instance of
mercadopago.Error. The error
object contains the following attributes:
||The HTTP status_code returned by the API, if applicable.|
||The error code returned by the API, if applicable.|
||The full JSON response returned by the API, if applicable.|
The specific subclass raised depends on the HTTP status code.
Running the tests
Make sure tests pass before contributing a bugfix or a new feature. To run the test suite, execute this in your terminal:
python setup.py test
This will execute the tests with your default Python interpreter.
tox to run the tests in all supported Python versions.
- Implement idempotency headers in POST/PUT requests.
- Implement retry request from error.
For more information about the API, refer to the official docs.