Programmatically make Amazon purchase?
Asked Answered
C

3

72

Is anyone aware of an Amazon API that allows you to purchase items programmatically?

I looked at tons of their web services (Product Advertising, MWS, etc.), and none of them seemed to do this.

I found this thread that seemed to suggest there was no solution, but there was no definitive answer: https://forums.aws.amazon.com/thread.jspa?threadID=45127

Just to be clear, I want to make a purchase with MY payment details, not on behalf of any user. I figured if ANYONE supported this, it would be Amazon. But I realize there may be business (security?) reasons not to support this as well...

Cyanide answered 28/1, 2012 at 21:24 Comment(1)
There is a third-party service (which I made) that provides an API for placing orders on Amazon. Zinc.ioIrmgard
A
27

UPDATE 2019/12/10 Even though my answer was accepted as the best answer it's outdated. Please see this answer: https://mcmap.net/q/274155/-programmatically-make-amazon-purchase


It's not possible – I guess for a bunch of security reasons. Read through the thread you posted in your question ;)

Lorraine@AWS, Aug 13, 2010 2:02 PM

Amazon does not offer APIs to place a customer order. Neither a Corporate Account or the Associate Program will allow you to build the type of solution you are describing.

Lorraine@AWS, May 13, 2011 4:17 PM

Amazon still does not offer this service, to the best of my knowledge, and this isn't something I would ever expect to see included in the Fulfillment Web Service which is specific to the FBA program.

Allhallowtide answered 5/6, 2012 at 15:10 Comment(4)
@moosgummi your answer is outdated as of 2012.Virtues
@Virtues is it outdated or not? The year is 2016 and have Amazon provided with an API for placing orders? I've seen their Order API, but it seems to only provide readable actions, not writable.Tutor
@MarcoAurélioDeleu I think the Amazon Product Advertising API (docs.aws.amazon.com/AWSECommerceService/latest/DG/Welcome.html) is still the best way to go. Although your potential customer has to checkout out via amazon.com itself.Allhallowtide
It is possible with a business relationship with Amazon. See my answer.Evaleen
L
40

http://www.zinc.io/ shows that it is, in fact, possible to do this.

I've read around the net that you could do this by sending crafted _GET responses, although I, myself, am currently trying to figure out which ones to send.

Lincoln answered 19/6, 2013 at 0:4 Comment(22)
UPDATE: As of December 2013, I created a complete auto-purchase bot for Amazon that also applies giftcards, credit cards, etc. So I know for a fact it is possible. I used CasperJS to do it...Lincoln
Using CasperJS to simulate user activity on the website is not the same thing as Amazon.com providing an API for automating purchases. It's fragile. Your bot can break if Amazon changes their markup.Makeshift
Yeah, no doubt. That's why I have upwards of 100% test code coverage on the beast with 24x7 text message alerts if something breaks ;-) Still, it is a programatic solution.Lincoln
Hey @TheodoreR.Smith did you by any chance open source that? Checked your github but could not find anything related.Zulema
@TheodoreR.Smith Any chance on opensourcing that bot?Aiguillette
@AdamMoisa Do you know how much I license this out to people?! Holy hell! It's worth at least 5 figures.Lincoln
I get that this is quite an old question but the topic is still relevant! I'd like to be able to order programmatically but cannot see where to begin, as Zinc is awfully expensive. Has anyone got any suggestions?Bioecology
@AdamMoisa there are some other open sourced amazon bots based on CasperJS. google.com/search?q=amazon+purchase+casperjs+githubHydrangea
The non-compete / non-disclosure agreement I signed in 2015 is about to expire. I'll recreate it open sourced then.Lincoln
@TheodoreR.Smith very interested in what you have for an open source solution!Froehlich
The NDA expires on 15 November 2020 and I plan on pouring work into it. I've made a prototype already. Automation tech GREATLY improved since 2013... I've already made a functional autobuyer for walmart.com, but I don't know if there's interest.Lincoln
@TheodoreR.Smith -- do you still have this script for Amazon? I am willing to pay. Need to place 25,000 orders.Edla
Heads up!!!!! My IP lawyer has certified that I can start deving this in earnest on 21 December 2020 :P 16 days from now! I've already abstracted out the generic WebBot and will be open sourcing that before Christmas!Lincoln
I will have an automated Amazon Login bot open sourced before 15 January 2021 and an Amazon Regsitration bot a few days later. By then, i'll have the GitHub project all set up and you guys can star + watch it...Lincoln
@TheodoreR.Smith, is the bot available?Premillennial
@TheodoreR.Smith I don't suppose this is available yet, is it? It would be tremendously helpful! Thanks!Bioecology
@TheodoreR.Smith what's the latest on your autobuyer? Did you open source that already?Perineurium
@TheodoreR.Smith any news?Arri
@theodore Free the botInfusive
@TheodoreR.Smith just keeping the alerts coming. Looks like there's a lot of need for thisLever
Casperjs/etc bots are not that hard to make, but amazon does change the forms pretty often, so they require a lot of work to keep runningSivie
I posted an updated answer here. You can do this via the Rye API nowBulgar
A
27

UPDATE 2019/12/10 Even though my answer was accepted as the best answer it's outdated. Please see this answer: https://mcmap.net/q/274155/-programmatically-make-amazon-purchase


It's not possible – I guess for a bunch of security reasons. Read through the thread you posted in your question ;)

Lorraine@AWS, Aug 13, 2010 2:02 PM

Amazon does not offer APIs to place a customer order. Neither a Corporate Account or the Associate Program will allow you to build the type of solution you are describing.

Lorraine@AWS, May 13, 2011 4:17 PM

Amazon still does not offer this service, to the best of my knowledge, and this isn't something I would ever expect to see included in the Fulfillment Web Service which is specific to the FBA program.

Allhallowtide answered 5/6, 2012 at 15:10 Comment(4)
@moosgummi your answer is outdated as of 2012.Virtues
@Virtues is it outdated or not? The year is 2016 and have Amazon provided with an API for placing orders? I've seen their Order API, but it seems to only provide readable actions, not writable.Tutor
@MarcoAurélioDeleu I think the Amazon Product Advertising API (docs.aws.amazon.com/AWSECommerceService/latest/DG/Welcome.html) is still the best way to go. Although your potential customer has to checkout out via amazon.com itself.Allhallowtide
It is possible with a business relationship with Amazon. See my answer.Evaleen
B
0

You can do this via the Rye API

Python example

Make sure you replace the API key values and the address information

import requests


# Constants from console.rye.com, replace all the dummy values below.
RYE_API_KEY = 'Basic UllFLL3VjYTlxMU0NjI1YzQwNzZiMWI1Og==' # Change me
RYE_SHOPPER_IP = '181.12.22.41' # Change me
RYE_CARD_TOKENIZATION_SECRET = 'Basic UW9nRjRoeHxMydGl6Y2NoMjCxRLNmI5RSWp5MGxmO221lJVRldKF0ZMGFsdvDF3ZdjRVrdk5CVTNJSDWecDhB2M0dXYWFBaDNjxdjJXaDF3dTU0eXE1Y200d24xcUU0bXFIeEpWNllUWXE=' # Change me
RYE_API_URL = 'https://graphql.api.rye.com/v1/query'
SPREEDLY_API_URL = 'https://core.spreedly.com/v1/payment_methods.json'

def create_cart(product_id, quantity, buyer_info):
    """
    Create a cart with the specified product and buyer information.
    
    Args:
        product_id (str): The ID of the product to add to the cart.
        quantity (int): The quantity of the product to add to the cart.
        buyer_info (dict): A dictionary containing buyer information.
        
    Returns:
        dict: The response from the Rye API containing the cart ID and any errors.
    """
    query = """
    mutation ($input: CartCreateInput!) {
        createCart(input: $input) {
            cart {
                id
            }
            errors {
                code
                message
            }
        }
    }
    """
    variables = {
        "input": {
            "items": {
                "amazonCartItemsInput": [{
                    "quantity": quantity,
                    "productId": product_id
                }]
            },
            "buyerIdentity": buyer_info
        }
    }
    response = requests.post(
        RYE_API_URL,
        headers={
            'Content-Type': 'application/json',
            'Authorization': RYE_API_KEY,
            'Rye-Shopper-IP': RYE_SHOPPER_IP
        },
        json={
            'query': query,
            'variables': variables
        }
    )
    return response.json()

def tokenize_card(card_info):
    """
    Tokenize a credit card using the Spreedly API.
    
    Args:
        card_info (dict): A dictionary containing credit card information.
        
    Returns:
        dict: The response from the Spreedly API containing the payment method token.
    """
    payload = {
        'payment_method': {
            'credit_card': card_info,
            'retained': True
        }
    }
    response = requests.post(
        SPREEDLY_API_URL,
        headers={
            'Authorization': RYE_CARD_TOKENIZATION_SECRET,
            'Content-Type': 'application/json'
        },
        json=payload
    )
    return response.json()

def submit_cart(cart_id, payment_method_token):
    """
    Submit a cart with the specified cart ID and payment method token.
    
    Args:
        cart_id (str): The ID of the cart to submit.
        payment_method_token (str): The token of the payment method to use.
        
    Returns:
        dict: The response from the Rye API containing the submission status and any errors.
    """
    query = """
    mutation ($input: CartSubmitInput!) {
        submitCart(input: $input) {
            cart {
                id
                stores {
                    isSubmitted
                    errors {
                        code
                        message
                    }
                }
            }
            errors {
                code
                message
            }
        }
    }
    """
    variables = {
        'input': {
            'id': cart_id,
            'token': payment_method_token
        }
    }
    response = requests.post(
        RYE_API_URL,
        headers={
            'Content-Type': 'application/json',
            'Authorization': RYE_API_KEY,
            'Rye-Shopper-IP': RYE_SHOPPER_IP
        },
        json={
            'query': query,
            'variables': variables
        }
    )
    return response.json()

def main():
    # Buyer information
    buyer_info = {
        "firstName": "John",
        "lastName": "Smith",
        "email": "[email protected]",
        "phone": "+14081234567",
        "address1": "100 Main St.",
        "address2": "",
        "city": "Berkeley",
        "provinceCode": "CA",
        "countryCode": "US",
        "postalCode": "94708"
    }
    
    # Create a cart
    product_id = "B07Z4L57XQ"
    quantity = 1
    create_cart_response = create_cart(product_id, quantity, buyer_info)
    cart_id = create_cart_response['data']['createCart']['cart']['id']
    errors = create_cart_response['data']['createCart']['errors']
    
    if cart_id and not errors:
        print('✅ Cart successfully created!')
    else:
        print('❌ Error: Unable to create cart.')
        print('Response from server:')
        print(create_cart_response)
        return
    
    # Tokenize credit card
    card_info = {
        "first_name": "John",
        "last_name": "Smith",
        "number": "4242424242424242",
        "verification_value": "181",
        "month": "04",
        "year": "2029"
    }
    tokenization_response = tokenize_card(card_info)
    payment_method_token = tokenization_response.get('transaction', {}).get('payment_method', {}).get('token')
    
    if payment_method_token:
        print('✅ Card successfully tokenized!')
    else:
        print('❌ Error: Unable to tokenize card.')
        print('Response from server:')
        print(tokenization_response)
        return
    
    # Submit cart
    submit_cart_response = submit_cart(cart_id, payment_method_token)
    is_submitted = submit_cart_response['data']['submitCart']['cart']['stores'][0]['isSubmitted']
    errors = submit_cart_response['data']['submitCart']['errors']
    
    if is_submitted and not errors:
        print('🎉 Hooray! You ordered an Amazon product via Python!')
        print('You can view your order on https://console.rye.com/orders')
    else:
        print('❌ Error! Unable to submit cart.')
        print('Response from server:')
        print(submit_cart_response)
        
if __name__ == '__main__':
    main()
Bulgar answered 11/4 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.