Skip to content
Justin Digs Data
Justin Digs Data
  • Home
  • Nerd Library
Justin Digs Data
Google API Gateway Logo

API Key Authentication with Google Cloud API Gateway and Cloud Run

Posted on February 10, 2024February 15, 2024 By Justin

Table of Contents

Toggle
  • Introduction
      • WARNING
  • Basic Steps
  • Prerequisites
  • Google Cloud Setup
    • Enable Needed Google Cloud Services
      • Console Method
      • Command Line Method
    • Create a Google Cloud Service Account
      • Console Method
      • Command Line Method
  • Backend API Creation
    • Create a simple Python-based API
      • main.py
      • requirements.txt
      • Dockerfile
      • start.sh
    • Deploy Code as a Docker Container to Cloud Run
    • Testing
    • Reenable Authentication
    • Grant Service Account Permissions
  • API Gateway Setup
    • Create API Gateway API
    • Testing Your API
      • Cloud Run URL
      • API Gateway URL
      • GET Tests
      • POST Tests
  • Conclusion

API Gateway Setup

That that all that preliminary stuff is out of the way, we can get to the actual point of this post: setting up the Google API Gateway.

Just to review, the API Gateway is going to sit between our users/clients and the Google Cloud Run service we created above. Instead of sending requests to the Cloud Run URLs we used previously, we’ll send them to the new URLs that we’ll create in the API Gateway setup process.

And for authentication, we’ll set up the Gateway to use API keys. Granted, it’s not as secure as other options, but there are times when we need it.

Before we get to creating that API key, we need to do four things:

  1. Create an API Gateway API
  2. Create an API Gateway Config
  3. Create an API Gateway
  4. Enable the API Gateway API

Create API Gateway API

Creating an API is pretty easy using the gcloud Google Command Line Interface:

gcloud api-gateway apis create {{API_ID}} --project={{PROJECT_ID}}

Where:

  • {{API_ID}}: Name/ID of the new API Gateway API we want to create (e.g. demo-api). The name must:
    • Have a maximum length of 63 characters
    • Contain only lowercase letters, numbers, or dashes (no underscores)
    • Not start with a dash
  • {{PROJECT_ID}} = Name of the Google Cloud project you’re using for the tutorial (e.g. api-gateway-key-auth-demo)

The process may take a few minutes to complete, so don’t worry if you find yourself staring at the “Waiting for API {{API_ID}} to be create…” message for a little bit.

Once it completes, you can list all your APIs by using the gcloud api-gateway apis list –project={{PROJECT_ID}} command:

and find details about a specific API using gcloud api-gateway apis describe {{API_ID}} –project={{PROJECT_ID}} :

Take note of that managedService value. You’ll need it later.

Pages: 1 2 3 4 5 6 7 8 9 10 11 12
Backend Stuff APICloud RunGCPPythonTutorial

Post navigation

Next post

Categories

  • AI
  • Backend Stuff

Recent Posts

  • An LLM’s Blog Post about Why You Shouldn’t Use LLMs to Write Blog Posts
  • API Key Authentication with Google Cloud API Gateway and Cloud Run

Archives

  • February 2024
©2025 Justin Digs Data | WordPress Theme by SuperbThemes