skip to Main Content

How to fetch all the products associated with my seller account through the SP-api in amazon?

2

Answers


  1. Using the Amazon portal, generate a refresh token (Atzr|....). Note your client ID and client secret as well.
    Pass it as a parameter to the following function:

    define('CLIENT_ID','amzn1.application-oa2-client.....');
    define('CLIENT_SECRET','amzn1.oa2-cs.v1.....');
    
    function writeLog($s,$level = 'INFO') {
        print($level . "t" . $s . "rn");
    }
    
    function getToken($refresh_token) {
        try {
            $url = sprintf('https://api.amazon.com/auth/o2/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s',$refresh_token,CLIENT_ID,CLIENT_SECRET);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, array());
            $result = curl_exec($ch);
            $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            if ($responseCode > 300) {
                throw new Exception($result, $responseCode);
            }
            return json_decode($result)->access_token;
        } catch(Exception $ex) {
            writeLog($ex->getMessage(),'ERROR');
        }
    }
    $your_auth_token = getToken('Atzr|...');
    

    Once you have the authorization token, use it to retrieve data from your desired endpoint, e.g.:

    function http_request(string $url, string $token, array $params = array()) {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","User-Agent: YourSuperApp","x-amz-access-token: $token")); // Add the token to the header
            if (!empty($params)) { // this is a POST, add body. 
                //Some endpoints expect POST instead of GET, there you can pass the array of values as the 3rd parameter of this function
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
            }
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($ch);
            if ($result === false) {
                throw new Exception(curl_error($ch), curl_errno($ch));
            }
            return json_decode($result);
        } catch(Exception $ex) {
            writeLog($ex->getMessage(),'ERROR');
        }
    }
    
    $items = http_request('https://sellingpartnerapi-eu.amazon.com/catalog/2022-04-01/items?marketplaceIds=["A1PA6795UKMFR9"]',$your_auth_token);
    

    Then you can process the JSON which should be in this format. You might use var_dump($items); to see how it looks like.

    Things to keep in mind:

    1. API URLs are different per region, see here. I used the EU one.
    2. Marketplace IDs can be found here. I used the german one.
    Login or Signup to reply.
  2. If you’re going to be doing serious work with SP API, I highly recommend you use a pre-built library. I’m a big fan of selling-partner-api PHP library, which is Laravel compatible. I’ve been using it for years in multiple projects and it simplifies the process for getting the Amazon product catalog and many other SP API related functions. Especially, if you’re building a public app and need to deal with account authorizations, permissions, etc. I’ve been using it for creating and processing FBA shipments without any issues.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search