Amazon Auth - Sign in to Amazon’s consumer website from Python


https://img.shields.io/pypi/v/amazon-auth https://img.shields.io/pypi/pyversions/amazon-auth.svg https://img.shields.io/codecov/c/github/alexdlaird/amazon-auth https://img.shields.io/github/actions/workflow/status/alexdlaird/amazon-auth/build.yml https://img.shields.io/readthedocs/amazon-auth https://img.shields.io/github/license/alexdlaird/amazon-auth

amazon-auth is an unofficial library that signs in to Amazon’s consumer website and persists the session, so it can be reused from Python. It handles sign-in, OTP and MFA, challenges, and browser sign-in.

Note

This package works by parsing Amazon’s consumer-facing website. As Amazon provides no official API to use, older versions of this package may break at any time, so it’s recommended that you use the latest version.

Installation

amazon-auth is available on PyPI and can be installed and/or upgraded using pip:

pip install amazon-auth --upgrade

That’s it! amazon-auth is now available as a package to your Python projects.

If pinning, be sure to use a wildcard for the minor version (e.g. ==0.2.*, not ==0.2.0) to ensure you always get the latest stable release.

Basic Usage

You’ll use AmazonSession to sign in to your Amazon account, then make requests with the signed-in session through get and post, which return the response alongside its parsed HTML.

from amazonauth.session import AmazonSession

amazon_session = AmazonSession("<AMAZON_EMAIL>",
                               "<AMAZON_PASSWORD>")
amazon_session.login()

response = amazon_session.get("https://www.amazon.com/gp/css/homepage.html")
print(response.parsed.title.text)

Building on amazon-auth

Packages that read more of Amazon’s website, like amazon-orders, are built on amazon-auth by subclassing its config, session, constants and selectors, and registering its CLI commands. See Building on amazon-auth.

Secure Sign-In

The most secure way to use amazon-auth is to sign in on Amazon’s own page, in a browser window, so your credentials never need to be stored:

from amazonauth.contrib.browser.playwright import PlaywrightBrowserLogin
from amazonauth.session import AmazonSession

amazon_session = AmazonSession()
PlaywrightBrowserLogin(amazon_session).login()

The session is persisted, so later sign-ins need no credentials:

from amazonauth.session import AmazonSession

amazon_session = AmazonSession()
amazon_session.login()

This requires a display and the browser extra (see Browser Automation to install it).

Automating Authentication

Authentication can be automated by (in order of precedence) storing credentials in environment variables, passing them to AmazonSession, or storing them in AmazonAuthConfig. The environment variables amazon-auth looks for are:

  • AMAZON_USERNAME

  • AMAZON_PASSWORD

  • AMAZON_OTP_SECRET_KEY (see otp_secret_key)

Any other config value can be set the same way, as AMAZON_<KEY> (see AmazonAuthConfig).

A configured username also applies to PlaywrightBrowserLogin: it’s filled in and submitted for you, so the browser opens on Amazon’s password or passkey step. The browser never fills in your password.

Handling Challenges

To enable WAF auto-solve via a third-party integration, install with the relevant extra:

pip install amazon-auth[capsolver]
pip install amazon-auth[anticaptcha]
pip install amazon-auth[2captcha]

See Solving WAF Challenges for details.

To enable browser-based challenge handling (ACIC and JavaScript bot-detection pages) via a headless browser, install with the browser extra:

pip install amazon-auth[browser]

See Browser Automation for details.

For legacy Captcha auto-solve on Python <=3.12, install with captcha extra:

pip install amazon-auth[captcha]

See Login Challenges for details.

Sites

amazon-auth signs in to amazon.com by default. To sign in to another Amazon site, set the domain in the config (e.g. amazon.co.uk, or amazon.de).

Each site has its own sign-in, so each keeps its own session: amazon.com’s in cookies.json in the config directory, and every other site’s beside it, named for the site (e.g. cookies.amazon.de.json). Signing in with --domain amazon.de leaves the amazon.com session as it is. An explicit cookie_jar_path is used for every site.

A domain can be any of these Amazon sites:

Site

English

amazon.ae

en-AE

amazon.ca

en-CA

amazon.co.jp

en-US

amazon.co.uk

en-GB

amazon.co.za

en-ZA

amazon.com

en-US

amazon.com.au

en-AU

amazon.com.be

en-GB

amazon.com.br

—

amazon.com.mx

—

amazon.com.tr

—

amazon.de

en-GB

amazon.eg

en-AE

amazon.es

en-GB

amazon.fr

en-GB

amazon.ie

en-IE

amazon.in

en-IN

amazon.it

en-GB

amazon.nl

en-GB

amazon.pl

—

amazon.sa

en-AE

amazon.se

en-GB

amazon.sg

en-SG

Known Limitations

  • Amazon Business accounts are not supported

Dive Deeper

For more advanced usage, dive deeper in to the rest of the documentation.