Creating and managing your product resources

After you have created a product set, you can create products and add them to the product set. When you create a product, you must provide a display name for the product and a product category. Currently supported categories are: homegoods or apparel.

You can also supply an optional description of your product, and optional labels for your product. Labels are key/value pairs that describe your product such as color=black or style=mens. You can include labels to filter the results of a product search to only search certain product images.

Creating a product

You can use online import to create an individual product. After a product is created you can then add a reference image to it or add it to a product set.

Command-line

To get the information for a product set, send a request to the following URI.

  • Replace project-id with the id of your Google Cloud Platform (GCP) project.

  • Replace location-id with a valid location identifier. Valid location identifiers are: us-west1, us-east1, europe-west1, and asia-east1.

POST https://vision.googleapis.com/v1p3beta1/projects/project-id/locations/location-id/products
{
  'displayName': 'sample-product-1234',
  'description': 'Athletic shorts',
  'productCategory': 'apparel',
  'productLabels': [
      { 'key': 'style', 'value': 'womens' },
      { 'key': 'color', 'value': 'blue' }
  ]
}

Python

from google.cloud import vision_v1p3beta1 as vision

def create_product(
        project_id, location, product_id, product_display_name,
        product_category):
    """Create one product.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_display_name: Display name of the product.
        product_category: Category of the product.
    """
    client = vision.ProductSearchClient()

    # A resource that represents Google Cloud Platform location.
    location_path = client.location_path(project=project_id, location=location)

    # Create a product with the product specification in the region.
    # Set product name and product display name.
    product = vision.types.Product(
        display_name=product_display_name,
        product_category=product_category)

    # The response is the product with the `name` field populated.
    response = client.create_product(
        parent=location_path,
        product=product,
        product_id=product_id)

    # Display the product information.
    print('Product name: {}'.format(response.name))

Java

/**
 * Create one product.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productId - Id of the product.
 * @param productDisplayName - Display name of the product.
 * @param productCategory - Category of the product.
 * @throws IOException - on I/O errors.
 */
public static void createProduct(
    String projectId,
    String computeRegion,
    String productId,
    String productDisplayName,
    String productCategory)
    throws IOException {
  ProductSearchClient client = ProductSearchClient.create();

  // A resource that represents Google Cloud Platform location.
  LocationName projectLocation = LocationName.of(projectId, computeRegion);

  // Create a product with the product specification in the region.
  // Multiple labels are also supported.
  Product myProduct =
      Product.newBuilder()
          .setName(productId)
          .setDisplayName(productDisplayName)
          .setProductCategory(productCategory)
          .build();
  Product product = client.createProduct(projectLocation.toString(), myProduct, productId);

  // Display the product information
  System.out.println(String.format("Product name: %s", product.getName()));
}

Node.js

// Imports the Google Cloud client library
const vision = require('@google-cloud/vision').v1p3beta1;

// Creates a client
const client = new vision.ProductSearchClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const projectId = 'Your Google Cloud project Id';
// const location = 'A compute region name';
// const productId = 'Id of the product';
// const productDisplayName = 'Display name of the product';
// const productCategory = 'Catoegory of the product';

// Resource path that represents Google Cloud Platform location.
const locationPath = client.locationPath(projectId, location);

const product = {
  displayName: productDisplayName,
  productCategory: productCategory,
};

const request = {
  parent: locationPath,
  product: product,
  productId: productId,
};

client
  .createProduct(request)
  .then(results => {
    // The response is the product with the `name` field populated
    const createdProduct = results[0];
    console.log(`Product name: ${createdProduct.name}`);
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

Adding a product to a product set

When you have a product and product set available you can then add the product to the product set.

Command-line

To add a product to a product set, send a POST request to the following URI.

  • Replace project-id with the id of your Google Cloud Platform (GCP) project.

  • Replace location-id with a valid location identifier. Valid location identifiers are: us-west1, us-east1, europe-west1, and asia-east1.

  • Replace product-set-id with the id for the product set to associate with the product.

  • Replace product-id with the id for the product to add.

POST https://vision.googleapis.com/v1p3beta1/projects/project-id/locations/location-id/productSets/product-set-id:addProduct
{
  'product': 'product-id'
}

Python

from google.cloud import vision_v1p3beta1 as vision

def add_product_to_product_set(
        project_id, location, product_id, product_set_id):
    """Add a product to a product set.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_set_id: Id of the product set.
    """
    client = vision.ProductSearchClient()

    # Get the full path of the product set.
    product_set_path = client.product_set_path(
        project=project_id, location=location,
        product_set=product_set_id)

    # Get the full path of the product.
    product_path = client.product_path(
        project=project_id, location=location, product=product_id)

    # Add the product to the product set.
    client.add_product_to_product_set(
        name=product_set_path, product=product_path)
    print('Product added to product set.')

Java

/**
 * Add a product to a product set.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productId - Id of the product.
 * @param productSetId - Id of the product set.
 * @throws IOException - on I/O errors.
 */
public static void addProductToProductSet(
    String projectId, String computeRegion, String productId, String productSetId)
    throws IOException {
  ProductSearchClient client = ProductSearchClient.create();

  // Get the full path of the product set.
  ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);

  // Get the full path of the product.
  String productPath = ProductName.of(projectId, computeRegion, productId).toString();

  // Add the product to the product set.
  client.addProductToProductSet(productSetPath, productPath);

  System.out.println(String.format("Product added to product set."));
}

Node.js

const vision = require('@google-cloud/vision').v1p3beta1;

const client = new vision.ProductSearchClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const projectId = 'Your Google Cloud project Id';
// const location = 'A compute region name';
// const productId = 'Id of the product';
// const productSetId = 'Id of the product set';

const productPath = client.productPath(projectId, location, productId);
const productSetPath = client.productSetPath(
  projectId,
  location,
  productSetId
);

const request = {
  name: productSetPath,
  product: productPath,
};

client
  .addProductToProductSet(request)
  .then(() => {
    console.log(`Product added to product set.`);
  })
  .catch(err => {
    console.error(err);
  });

Removing a product from a product set

You can also remove an existing product from a product set.

Command-line

To remove a product from a product set, send a POST request to the following URI.

  • Replace project-id with the id of your Google Cloud Platform (GCP) project.

  • Replace location-id with a valid location identifier. Valid location identifiers are: us-west1, us-east1, europe-west1, and asia-east1.

  • Replace product-set-id with the id for the product set that is associated with the product.

  • Replace product-id with the id for the product to remove.

POST https://vision.googleapis.com/v1p3beta1/projects/project-id/locations/location-id/productSets/product-set-id:removeProduct
{
  'product': 'product-id'
}

Python

from google.cloud import vision_v1p3beta1 as vision

def remove_product_from_product_set(
        project_id, location, product_id, product_set_id):
    """Remove a product from a product set.
    Args:
        project_id: Id of the project.
        location: A compute region name.
        product_id: Id of the product.
        product_set_id: Id of the product set.
    """
    client = vision.ProductSearchClient()

    # Get the full path of the product set.
    product_set_path = client.product_set_path(
        project=project_id, location=location,
        product_set=product_set_id)

    # Get the full path of the product.
    product_path = client.product_path(
        project=project_id, location=location, product=product_id)

    # Remove the product from the product set.
    client.remove_product_from_product_set(
        name=product_set_path, product=product_path)
    print('Product removed from product set.')

Java

/**
 * Remove a product from a product set.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productId - Id of the product.
 * @param productSetId - Id of the product set.
 * @throws IOException - on I/O errors.
 */
public static void removeProductFromProductSet(
    String projectId, String computeRegion, String productId, String productSetId)
    throws IOException {
  ProductSearchClient client = ProductSearchClient.create();

  // Get the full path of the product set.
  ProductSetName productSetPath = ProductSetName.of(projectId, computeRegion, productSetId);

  // Get the full path of the product.
  String productPath = ProductName.of(projectId, computeRegion, productId).toString();

  // Remove the product from the product set.
  client.removeProductFromProductSet(productSetPath, productPath);

  System.out.println(String.format("Product removed from product set."));
}

Node.js

const vision = require('@google-cloud/vision').v1p3beta1;

const client = new vision.ProductSearchClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
const productSetPath = client.productSetPath(
  projectId,
  location,
  productSetId
);

const productPath = client.productPath(projectId, location, productId);

const request = {
  name: productSetPath,
  product: productPath,
};

client
  .removeProductFromProductSet(request)
  .then(() => {
    console.log(`Product removed from product set.`);
  })
  .catch(err => {
    console.error(err);
  });

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Vision API
Need help? Visit our support page.