An API wrapper for MercadoPago
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 CLIENT_ID and CLIENT_SECRET here.
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) /customers or /v1/customers, you can do:
- List all customers. Pass pagination parameters via keyword arguments.
- Create a new Customer.
- Delete a Customer.
- Update a Customer, include ìd in 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 mercadopago.api module.
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 mercadopago.Error otherwise.
Every API call you make will return a Response instance with the following attributes:
|status_code||The HTTP status_code returned by the API.|
|data||The decoded JSON response returned by the API.|
|url||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 .data which 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 PaginatedResponse.
- Requests the next page and returns a PaginatedResponse.
- 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:
|http_status||The HTTP status_code returned by the API, if applicable.|
|code||The error code returned by the API, if applicable.|
|json_data||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. Use 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.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size pymercadopago-0.3.0.tar.gz (9.0 kB)||File type Source||Python version None||Upload date||Hashes View|