# Make an API call

The [googleads/googleads-shopping-samples](https://github.com/googleads/googleads-shopping-samples)
repo on GitHub contains sample code for common operations for each client
library. For example, the samples in
**googleads-shopping-samples/python/shopping/content/products/** provide code
for common operations using the
[`products`](https://developers.google.com/shopping-content/reference/rest/v2.1/products)
resource with Python. In this guide, you start with an empty file and build
up an example that inserts a new product, so that you can see the basic
structure and required components of applications that integrate with the
Content API. The end result will be similar to the example in the
**products/insert.py** sample file. You can then use the
[API Explorer](https://developers.google.com/shopping-content/reference/rest/v2.1/products/list?apix=true)
for the `products.list` method to verify that the product was added successfully.

> [!NOTE]
> **Note:** This example uses the **python/shopping/content/common.py** module to create a service object that handles authentication and provides an interface for making calls to the Content API. The **common.py** module also includes other helpful methods for common operations, such as generating unique IDs, checking whether an account is a multi-client account (MCA), and retrying requests.

To make your first call, complete the following steps:

1. In the **googleads-shopping-samples/python/shopping/content/products/**
   directory, create an empty **my-insert.py** file. Add all of the code in the
   following steps to this file.

2. Add the import statements for the required modules.

   At the beginning of **my-insert.py**, add the following code:

       from __future__ import print_function
       import sys

       # The common module provides setup functionality used by the samples,
       # such as authentication and unique id generation.
       from shopping.content import common

3. Define a unique product ID and create a dictionary with the product
   definition.

   At the end of **my-insert.py**, add the following code:

       offer_id = 'book#%s' % common.get_unique_id()
       product = {
            'offerId':
                offer_id,
            'title':
                'A Tale of Two Cities',
            'description':
                'A classic novel about the French Revolution',
            'link':
                'http://my-book-shop.com/tale-of-two-cities.html',
            'imageLink':
                'http://my-book-shop.com/tale-of-two-cities.jpg',
            'contentLanguage':
                'en',
            'targetCountry':
                'US',
            'channel':
                'online',
            'availability':
                'in stock',
            'condition':
                'new',
            'googleProductCategory':
                'Media > Books',
            'gtin':
                '9780007350896',
            'price': {
                'value': '2.50',
                'currency': 'USD'
            },
            'shipping': [{
                'country': 'US',
                'service': 'Standard shipping',
                'price': {
                    'value': '0.99',
                    'currency': 'USD'
                }
            }],
            'shippingWeight': {
                'value': '200',
                'unit': 'grams'
            }
       }

4. Create a function that runs when the script is run from the command line. The
   function constructs a service object to interact with the Content API, gets the
   merchant ID from the configuration file, constructs the request, and executes
   the request to make the API call.

   At the end of **my-insert.py**, add the following code:

       def main(argv):
         # Construct the service object to interact with the Content API.
         service, config, _ = common.init(argv, __doc__)

         # Get the merchant ID from merchant-info.json.
         merchant_id = config['merchantId']

         # Create the request with the merchant ID and product object.
         request = service.products().insert(merchantId=merchant_id, body=product)

         # Execute the request and print the result.
         result = request.execute()
         print('Product with offerId "%s" was created.' % (result['offerId']))

       # Allow the function to be called with arguments passed from the command line.
       if __name__ == '__main__':
         main(sys.argv)

5. To run the script and execute the API call, from a terminal window, navigate
   to **googleads-shopping-samples/python/** and run:

       python -m shopping.content.products.my-insert

   If the call was successful, the service prints the following message to the terminal:
   **Product with offerId "<var translate="no">offerId</var>" was created.**
6. To verify that the product was added successfully, use the API Explorer for
   the `products.list` method to return all of the products in your Merchant Center
   account.

   > [!NOTE]
   > **Note:** It may take up to a few minutes after the `products.insert` call completes before the product is available through `products.list` or in Merchant Center.

   In the
   [API Explorer for the `products.list` method](https://developers.google.com/shopping-content/reference/rest/v2.1/products/list?apix=true),
   enter the following values:
   1. Enter your `merchantId`.

   > [!NOTE]
   > **Note:** All other request parameters are optional.

   1. In the **Credentials** section, select **Google OAuth 2.0** and **API key**.
   2. Click the **Execute** button.
   3. If prompted, sign in with the Google account associated with your Merchant Center account.

   If the product was added successfully, the product data appears in the API
   explorer response.

Merchants are responsible for complying with the [Shopping
ads](https://support.google.com/merchants/answer/6149970) and [free
listings](https://support.google.com/merchants/answer/9261529) policies. Google
Shopping reserves the right to enforce these policies and respond appropriately
if we find content or behavior that violates these policies.

> [!NOTE]
> **Note:** By default, new products inserted via the Content API are added to a new primary feed in Merchant Center. For more information on using different feed types with the Content API, see [Best Practices](https://developers.google.com/shopping-content/guides/best-practices).