This project demonstrates how to build a serverless API using GCP Cloud Functions and API Gateway that interfaces with Couchbase's Data API to manage airport data from the travel-sample dataset.
Note: The FTS features require:
- A Full Text Search index with geo-spatial mapping on hotel documents
- The travel-sample dataset with hotel documents in the
inventory.hotelcollection - Hotels must have geo coordinates (
geo.latandgeo.lonfields) for proximity search
- Node.js (v22.x or later)
- Google Cloud CLI configured with appropriate credentials.
- Google Cloud Project with Cloud Functions and API Gateway with services enabled.
- Couchbase Capella cluster with Data API enabled.
- Couchbase travel-sample bucket loaded.
-
Clone the repository
git clone https://github.com/couchbase-examples/couchbase-data-api-serverless-cookbook.git
-
Navigate to the gcp directory:
cd couchbase-data-api-serverless-cookbook/gcp -
Install dependencies:
npm install
-
Configure your environment variables: Copy the example environment file and update with your values:
cp .env.example .env
Update the
.envfile with your actual values:# Google Cloud Function Configuration PROJECT_ID=<gcp-project-id> REGION=europe-west1 MEMORY=256Mi TIMEOUT=120s RUNTIME=nodejs22 # Cluster Credentials DATA_API_ENDPOINT=<capella-data-api-endpoint> DATA_API_USERNAME=<capella-cluster-username> DATA_API_PASSWORD=<capella-cluster-password> DB_BUCKET_NAME=travel-sample DB_SCOPE=inventory DB_COLLECTION=airport
-
Authenticate with Google Cloud:
gcloud auth login
Before using the hotel search functionality, you need to create a Full Text Search index. Use the Node.js script provided in the root of the repository.
See ../scripts/README.md for detailed instructions on creating the required hotel-geo-index for geo-spatial hotel searches.
First, deploy your Cloud Functions:
npm run deploy-gcp-cloud-functionThis will deploy all functions defined in the FUNCTIONS array in config.js.
After functions are deployed, run the API Gateway deployment:
npm run deploy-api-gatewayNote: The API gateway deployment usually takes a few minutes to complete.
This script will:
- Create a new API Gateway
- Set up Cloud Function integrations
- Create a gateway and output the API endpoint URL
If you encounter 403 Forbidden errors when testing the API endpoints, grant the default Compute Engine service account permission to invoke Cloud Run. In this setup, API Gateway uses that identity under the hood, and this is required because Cloud Functions are deployed with authentication enabled by default
Grant the required permission:
# Get your project number
PROJECT_NUMBER=$(gcloud projects describe $(gcloud config get-value project) --format="value(projectNumber)")
# Grant Cloud Run Invoker role to the Compute Engine default service account
gcloud projects add-iam-policy-binding $(gcloud config get-value project) \
--member="serviceAccount:${PROJECT_NUMBER}-compute@developer.gserviceaccount.com" \
--role="roles/run.invoker"Alternative method - if you know your project number:
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:YOUR_PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
--role="roles/run.invoker"You can find your project number in the Google Cloud Console project settings or by running:
gcloud projects describe $(gcloud config get-value project) --format="value(projectNumber)"The integration tests will automatically discover your API Gateway endpoint and test all API operations through it:
npm run test