Smart API to predict customer propensity to purchase by using Apigee, BigQuery ML, and Spanner

Last reviewed 2023-06-20 UTC

This document describes how an API can be used to predict how likely a customer is to make a purchase.

This document is intended for API developers and data specialists who want to generate more revenue through omni-channel and ecommerce platforms by providing a more personalized experience to users. It assumes that you're familiar with Apigee, BigQuery ML, Spanner, the Google Cloud CLI, and Apache Maven.

The API that you create uses online predictions to take real-time action based on user behavior on your website. Online predictions combine machine learning (ML) insights with a product catalog and other information, which helps you to create a better experience for the customers across multiple channels.


The following diagram shows the architecture and process used in this solution:

Architecture of an API to predict customer propensity to purchase.

This process includes the following steps:

  1. The consumer app sends an HTTPS request to the API that's deployed on the Apigee platform. The request fetches the product recommendations data for the user who is logged in.
  2. An API proxy gets the product recommendations data for the user ID from a BigQuery dataset.
  3. The API proxy makes a callout to fetch the data from the product primary database on a Spanner instance.
  4. The API proxy aggregates the data from the product recommendations and the product primary datasets.
  5. A response is returned to the consumer app.

Design considerations

The following guidelines can help you to develop an architecture that meets your organization's requirements for security and cost.

Security, privacy, and compliance

To help protect your API from unauthorized access, you include an API proxy that requires an API key, as explained in the deployment procedure. When an app makes a request, the app must supply a valid key. If the key is valid, the request is allowed. If the key is invalid, the request results in an authorization failure.

As an option, you can create an AppSheet app to show product recommendations to ecommerce website users. AppSheet also uses the API proxy as the data source for your new app. For more information, see Optional: Create an AppSheet app using Apigee as a data source.

Cost optimization

In this document, you use the following billable components of Google Cloud:

  • BigQuery and BigQuery ML Flex Slots
  • Spanner
  • Apigee

To generate a cost estimate based on your projected usage, use the pricing calculator. New Google Cloud users might be eligible for a free trial.


To deploy this architecture, see Build a smart API to predict customer propensity to purchase by using Apigee, BigQuery ML, and Spanner.

What's Next?