Code Monkey home page Code Monkey logo

amazon-order-reports-api's Introduction

amazon-order-reports-api

Scrapes orders and refunds from Amazon.com.

There's no offical API to retrieve these, so this library uses Puppeteer internally to access them. As this isn't an officially supported API, it may break at any time. Puppeteer comes with some other caveats, notably when running in Docker. See Puppeteer's troubleshooting section if you run into issues.

This library has only been tested on Amazon.com with the language set to English. It's unknown if it will work in other regions/languages.

Installation

npm i amazon-order-reports-api

Usage

import { AmazonOrderReportsApi } from 'amazon-order-reports-api';

(async () => {
  const api = new AmazonOrderReportsApi({
    username: '[email protected]',
    password: 'password1234',
    // Or use otpFn
    otpSecret: 'USJF YSN7 87YR PP4D AN78 FAAF 81D8 1PU9 JJRF QP87 9UDM IO3W SJRY'
  });

  for await (const item of api.getItems({
    startDate: new Date('2020-10-01'),
    endDate: new Date('2020-12-31')
  })) {
    console.log(item);
  }

  for await (const refund of api.getRefunds({
    startDate: new Date('2020-10-01'),
    endDate: new Date('2020-12-31')
  })) {
    console.log(refund);
  }

  await api.stop();
})();

API

API docs are available on the docs site

Email notifications

As a side effect of generating an order report, Amazon will send an email notification that the order report is ready. This can generate a large volume of emails if reports are retrieved frequently. In many mail providers, an e-mail filter can be used to delete or move these emails. E.g. in Gmail:

from:([email protected]) subject:(Your order history report)

amazon-order-reports-api's People

Contributors

bvosk avatar semantic-release-bot avatar starsprung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

amazon-order-reports-api's Issues

Library fails when account has no refunds

I'm trying to use the YNAB script, and it fails with the following error:

{"level":"info","message":"Retrieving reports from Amazon","metadata":{},"timestamp":"2021-01-05T02:26:13.784Z"}
(node:23752) UnhandledPromiseRejectionWarning: Error: Invalid Record Length: columns length is 18, got 1 on line 2
    at Parser.__onRow (C:\snapshot\amazon-ynab-sync\node_modules\csv-parse\lib\index.js:762:9)
    at Parser.__parse (C:\snapshot\amazon-ynab-sync\node_modules\csv-parse\lib\index.js:635:40)
    at Parser._flush (C:\snapshot\amazon-ynab-sync\node_modules\csv-parse\lib\index.js:453:22)
    at Parser.prefinish (_stream_transform.js:142:10)
    at Parser.emit (events.js:315:20)
    at prefinish (_stream_writable.js:618:14)
    at finishMaybe (_stream_writable.js:626:5)
    at Parser.Writable.end (_stream_writable.js:570:5)
    at ReadStream.onend (_stream_readable.js:677:10)
    at Object.onceWrapper (events.js:421:28)

Looking at my Amazon order history reports, the only reports generated by the library were of type Refund. This is what one of them looks like:

Order ID,Order Date,Title,Category,ASIN/ISBN,Website,Purchase Order Number,Refund Date,Refund Condition,Refund Amount,Refund Tax Amount,Tax Exemption Applied,Refund Reason,Quantity,Seller,Seller Credentials,Buyer Name,Group Name
No data found for this time period

Does not work for me (updated order report site?)

First off, thank you for this awesome repo!

I'm trying to use this for YNAB sync along with your other repo - amazon-ynab-sync. This is a better-thought out solution than my approach to the same problem of categorizing Amazon purchases in YNAB. However when I run it under my account, I get the following output:

{"level":"info","message":"Retrieving reports from Amazon","metadata":{},"timestamp":"2022-06-25T12:39:26.229Z"}
(node:34621) UnhandledPromiseRejectionWarning: Error: No sign-out link detected, failed to sign in?
    at AmazonOrderReportsApi._assert (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/node_modules/amazon-order-reports-api/lib/index.js:198:19)
    at async AmazonOrderReportsApi._handleLogin (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/node_modules/amazon-order-reports-api/lib/index.js:331:9)
    at async AmazonOrderReportsApi._navigate (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/node_modules/amazon-order-reports-api/lib/index.js:360:9)
    at async AmazonOrderReportsApi._getReport (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/node_modules/amazon-order-reports-api/lib/index.js:265:9)
    at async AmazonOrderReportsApi.getItems (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/node_modules/amazon-order-reports-api/lib/index.js:124:9)
    at async Object.getAmazonTransactions (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/lib/amazon.js:72:26)
    at async Object.batch [as default] (/Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/node_modules/it-batch/index.js:20:20)
    at async /Users/brian/.nvm/versions/node/v14.17.1/lib/node_modules/amazon-ynab-sync/lib/index.js:21:22
(Use `node --trace-warnings ...` to show where the warning was created)
(node:34621) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:34621) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

When I run with the --debug-mode flag, I can see it get to the order reports screen. But then it gets stuck there. I'm assuming Amazon updated the order reports page and the selector needs to be updated.

Just wanted to open an issue and say that I'll be working on updates in a fork. Please let me know if you have any thoughts and if you are open to accepting a PR.

newbie question - how to create an otpFn that isn't skipped over?

Hello!

This might be a newbie question, but I am attempting to create an otpFn to prompt for the MFA code, and it seems to keep returning early:

(async () => {
  const api = new AmazonOrderReportsApi({
    logLevel: LogLevel.DEBUG,
    username: '[email protected]',
    password: 'pass',
    otpFn: async () => {
      let answer = '';
      const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
      });
      await rl.question('what is otp Code? ', (ans) => {
        answer = ans;
      });
      return answer;
    },
  });
  ... // rest omitted

What I get though is this in the log. It doesn't even wait for the prompt:

{"level":"debug","message":"Entering OTP code","metadata":{},"timestamp":"2022-01-04T01:36:36.285Z"}
what is otp Code? {"level":"debug","message":"nick remember device input","metadata":{},"timestamp":"2022-01-04T01:36:36.290Z"}
{"level":"debug","message":"Clicking input[type=submit]","metadata":{},"timestamp":"2022-01-04T01:36:36.309Z"}
{"level":"debug","message":"Account fixup","metadata":{},"timestamp":"2022-01-04T01:36:36.897Z"}
/Users/klauern/dev/js-ts/amazon-order-reports-api/lib/index.js:198
            throw new Error(message ?? `Assertion failed! No element matching selector: ${selector}`);
                  ^
Error: No sign-out link detected, failed to sign in?
... bunch of stacktrace
Waiting for the debugger to disconnect...

How to debug time out?

I successfully downloaded both Items and Refunds reports once. When I try it again, it always times out while downloading the Refunds report. I tried running with puppeteerOpts: { headless: false } to see what's happening but that stops immediately after logging in with Error: No sign-out link detected, failed to sign in?.

How do you debug issues?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.