Google Cloud Retail

2.10.0 · active · verified Thu Apr 16

The `google-cloud-retail` client library for Python allows developers to interact with the Google Cloud Retail API. This API provides AI-powered solutions for e-commerce, including personalized recommendations, search, and browsing experiences. As part of the broader `google-cloud-python` monorepo, it receives frequent updates, often with new features and occasional minor breaking changes for specific Python versions or API revisions. The current version is 2.10.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `RetailServiceClient` and list the catalogs configured for a Google Cloud project. It uses environment variables for project configuration and handles potential authentication issues. Replace `YOUR_PROJECT_NUMBER` or set `GCP_PROJECT_NUMBER` environment variable.

import os
from google.cloud.retail_v2 import RetailServiceClient

# Ensure GOOGLE_APPLICATION_CREDENTIALS is set or gcloud is authenticated
# For local development, run: gcloud auth application-default login

project_number = os.environ.get('GCP_PROJECT_NUMBER', 'YOUR_PROJECT_NUMBER') # e.g., '1234567890'
location_id = 'global' # or a specific region, e.g., 'us-central1'

try:
    client = RetailServiceClient()
    parent = f"projects/{project_number}/locations/{location_id}"
    
    print(f"Listing catalogs for parent: {parent}")
    response = client.list_catalogs(parent=parent)
    
    found_catalogs = False
    for catalog in response:
        print(f"  Catalog name: {catalog.name}, Display name: {catalog.display_name}")
        found_catalogs = True
    
    if not found_catalogs:
        print("No catalogs found. Ensure the project number and location are correct and the Retail API is enabled.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Make sure 'GCP_PROJECT_NUMBER' environment variable is set or replace 'YOUR_PROJECT_NUMBER'.")
    print("Also, ensure the Google Cloud Retail API is enabled for your project.")

view raw JSON →