Integrating with SDK

Here we have shared code samples to call SearchItems and GetItems operations using PAAPI SDK.

Java

In this section, we will share a simple Java program to create and send request for SearchItems and GetItems operations.

  • Create a simple Java project in your preferred IDE.
  • To get dependencies download zip file, extract contents and add them to your project's build path.
  • Download Java sample code snippets for each operation for Java SDK.

SearchItems operation sample

/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

/*
 * ProductAdvertisingAPI
 *
 * https://webservices.amazon.com/paapi5/documentation/index.html
 */

/*
 * This sample code snippet is for ProductAdvertisingAPI 5.0's SearchItems API
 * For more details, refer:
 * https://webservices.amazon.com/paapi5/documentation/search-items.html
 */

package samples;

import java.util.ArrayList;
import java.util.List;

import com.amazon.paapi5.v1.ApiClient;
import com.amazon.paapi5.v1.ApiException;
import com.amazon.paapi5.v1.ErrorData;
import com.amazon.paapi5.v1.Item;
import com.amazon.paapi5.v1.PartnerType;
import com.amazon.paapi5.v1.SearchItemsRequest;
import com.amazon.paapi5.v1.SearchItemsResource;
import com.amazon.paapi5.v1.SearchItemsResponse;
import com.amazon.paapi5.v1.api.DefaultApi;

public class SampleSearchItemsApi {

    public static void main(String[] args) {
        ApiClient client = new ApiClient();

        // Add your credentials
        // Please add your access key here
        // Please add your access key here
        client.setAccessKey("<YOUR-ACCESS-KEY-HERE>");
        // Please add your secret key here
        client.setSecretKey("<YOUR-SECRET-KEY-HERE>");

        // Enter your partner tag (store/tracking id)
        String partnerTag = "<YOUR-PARTNER-TAG-HERE>";

        /*
         * PAAPI Host and Region to which you want to send request. For more
         * details refer:
         * https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
         */
        client.setHost("webservices.amazon.com");
        client.setRegion("us-east-1");

        DefaultApi api = new DefaultApi(client);

        // Request initialization

        /*
         * Choose resources you want from SearchItemsResource enum For more
         * details, refer:
         * https://webservices.amazon.com/paapi5/documentation/search-items.html#resources-parameter
         */
        List<SearchItemsResource> searchItemsResources = new ArrayList<SearchItemsResource>();
        searchItemsResources.add(SearchItemsResource.ITEMINFO_TITLE);
        searchItemsResources.add(SearchItemsResource.OFFERS_LISTINGS_PRICE);

        /*
         * Specify the category in which search request is to be made
         * For more details, refer:
         * https://webservices.amazon.com/paapi5/documentation/use-cases/organization-of-items-on-amazon/search-index.html
         */
        String searchIndex = "All";

        // Specify keywords
        String keywords = "Harry Potter";

        // Sending the request
        SearchItemsRequest searchItemsRequest = new SearchItemsRequest().partnerTag(partnerTag).keywords(keywords)
                .searchIndex(searchIndex).resources(searchItemsResources).partnerType(PartnerType.ASSOCIATES);

        try {
            // Forming the request
            SearchItemsResponse response = api.searchItems(searchItemsRequest);

            System.out.println("API called successfully");
            System.out.println("Complete response: " + response);

            // Parsing the request
            if (response.getSearchResult() != null) {
                System.out.println("Printing first item information in SearchResult:");
                Item item = response.getSearchResult().getItems().get(0);
                if (item != null) {
                    if (item.getASIN() != null) {
                        System.out.println("ASIN: " + item.getASIN());
                    }
                    if (item.getDetailPageURL() != null) {
                        System.out.println("DetailPageURL: " + item.getDetailPageURL());
                    }
                    if (item.getItemInfo() != null && item.getItemInfo().getTitle() != null
                            && item.getItemInfo().getTitle().getDisplayValue() != null) {
                        System.out.println("Title: " + item.getItemInfo().getTitle().getDisplayValue());
                    }
                    if (item.getOffers() != null && item.getOffers().getListings() != null
                            && item.getOffers().getListings().get(0).getPrice() != null
                            && item.getOffers().getListings().get(0).getPrice().getDisplayAmount() != null) {
                        System.out.println(
                                "Buying price: " + item.getOffers().getListings().get(0).getPrice().getDisplayAmount());
                    }
                }
            }
            if (response.getErrors() != null) {
                System.out.println("Printing errors:\nPrinting Errors from list of Errors");
                for (ErrorData error : response.getErrors()) {
                    System.out.println("Error code: " + error.getCode());
                    System.out.println("Error message: " + error.getMessage());
                }
            }
        } catch (ApiException exception) {
            // Exception handling
            System.out.println("Error calling PA-API 5.0!");
            System.out.println("Status code: " + exception.getCode());
            System.out.println("Errors: " + exception.getResponseBody());
            System.out.println("Message: " + exception.getMessage());
            if (exception.getResponseHeaders() != null) {
                // Printing request reference
                System.out.println("Request ID: " + exception.getResponseHeaders().get("x-amzn-RequestId"));
            }
            // exception.printStackTrace();
        } catch (Exception exception) {
            System.out.println("Exception message: " + exception.getMessage());
            // exception.printStackTrace();
        }
    }
}

GetItems operation sample

/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

/*
 * ProductAdvertisingAPI
 *
 * https://webservices.amazon.com/paapi5/documentation/index.html
 */

/*
 * This sample code snippet is for ProductAdvertisingAPI 5.0's GetItems API
 * For more details, refer:
 * https://webservices.amazon.com/paapi5/documentation/get-items.html
 */

package samples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.amazon.paapi5.v1.ApiClient;
import com.amazon.paapi5.v1.ApiException;
import com.amazon.paapi5.v1.ErrorData;
import com.amazon.paapi5.v1.GetItemsRequest;
import com.amazon.paapi5.v1.GetItemsResource;
import com.amazon.paapi5.v1.GetItemsResponse;
import com.amazon.paapi5.v1.Item;
import com.amazon.paapi5.v1.PartnerType;
import com.amazon.paapi5.v1.api.DefaultApi;

public class SampleGetItemsApi {

    /**
     * Returns Item mapped to ASIN
     *
     * @param items Items
     * @return Items mapped to ASIN
     */
    private static Map<String, Item> parse_response(List<Item> items) {
        Map<String, Item> mappedResponse = new HashMap<String, Item>();
        for (Item item : items) {
            mappedResponse.put(item.getASIN(), item);
        }
        return mappedResponse;
    }

    public static void main(String[] args) {
        ApiClient client = new ApiClient();

        // Add your credentials
        // Please add your access key here
        client.setAccessKey("<YOUR-ACCESS-KEY-HERE>");
        // Please add your secret key here
        client.setSecretKey("<YOUR-SECRET-KEY-HERE>");

        // Enter your partner tag (store/tracking id)
        String partnerTag = "<YOUR-PARTNER-TAG-HERE>";

        /*
         * PAAPI Host and Region to which you want to send request. For more
         * details refer:
         * https://webservices.amazon.com/paapi5/documentation/common-request-parameters.html#host-and-region
         */
        client.setHost("webservices.amazon.com");
        client.setRegion("us-east-1");

        DefaultApi api = new DefaultApi(client);

        // Request initialization
        /*
         * Choose resources you want from GetItemsResource enum For more
         * details, refer:
         * https://webservices.amazon.com/paapi5/documentation/get-items.html#resources-parameter
         */
        List<GetItemsResource> getItemsResources = new ArrayList<GetItemsResource>();
        getItemsResources.add(GetItemsResource.ITEMINFO_TITLE);
        getItemsResources.add(GetItemsResource.OFFERS_LISTINGS_PRICE);

        // Choose item id(s)
        List<String> itemIds = new ArrayList<String>();
        itemIds.add("059035342X");
        itemIds.add("B00X4WHP55");
        itemIds.add("1401263119");

        // Forming the request
        GetItemsRequest getItemsRequest = new GetItemsRequest().itemIds(itemIds).partnerTag(partnerTag)
                .resources(getItemsResources).partnerType(PartnerType.ASSOCIATES);

        try {
            // Sending the request
            GetItemsResponse response = api.getItems(getItemsRequest);

            System.out.println("API called successfully");
            System.out.println("Complete response: " + response);

            // Parsing the request
            if (response.getItemsResult() != null) {
                System.out.println("Printing all item information in ItemsResult:");
                Map<String, Item> responseList = parse_response(response.getItemsResult().getItems());
                for (String itemId : itemIds) {
                    if (response.getItemsResult().getItems() != null) {
                        System.out.println("Printing information about the ASIN: " + itemId);
                        if (responseList.get(itemId) != null) {
                            Item item = responseList.get(itemId);
                            if (item.getASIN() != null) {
                                System.out.println("ASIN: " + item.getASIN());
                            }
                            if (item.getDetailPageURL() != null) {
                                System.out.println("DetailPageURL: " + item.getDetailPageURL());
                            }
                            if (item.getItemInfo() != null && item.getItemInfo().getTitle() != null
                                    && item.getItemInfo().getTitle().getDisplayValue() != null) {
                                System.out.println("Title: " + item.getItemInfo().getTitle().getDisplayValue());
                            }
                            if (item.getOffers() != null && item.getOffers().getListings() != null
                                    && item.getOffers().getListings().get(0).getPrice() != null
                                    && item.getOffers().getListings().get(0).getPrice().getDisplayAmount() != null) {
                                System.out.println("Buying price: "
                                        + item.getOffers().getListings().get(0).getPrice().getDisplayAmount());
                            }
                        } else {
                            System.out.println("Item not found, check errors");
                        }
                    }
                }
            }
            if (response.getErrors() != null) {
                System.out.println("Printing errors:\nPrinting Errors from list of Errors");
                for (ErrorData error : response.getErrors()) {
                    System.out.println("Error code: " + error.getCode());
                    System.out.println("Error message: " + error.getMessage());
                }
            }
        } catch (ApiException exception) {
            // Exception handling
            System.out.println("Error calling PA-API 5.0!");
            System.out.println("Status code: " + exception.getCode());
            System.out.println("Errors: " + exception.getResponseBody());
            System.out.println("Message: " + exception.getMessage());
            if (exception.getResponseHeaders() != null) {
                // Printing request reference
                System.out.println("Request ID: " + exception.getResponseHeaders().get("x-amzn-RequestId"));
            }
            // exception.printStackTrace();
        } catch (Exception exception) {
            System.out.println("Exception message: " + exception.getMessage());
            // exception.printStackTrace();
        }
    }
}

Node.Js

Download NodeJS sample code snippets for each operation for Node.Js SDK.

Python

Download Python sample code snippets for each operation for Python SDK.

PHP

Download PHP sample code snippets for each operation for PHP SDK.

results matching ""

    No results matching ""