fastapiopenapiapi-gatewaygoogle-cloud-api-gateway

FastAPI and GCP API Gateway


I have a backend developed in FastApi using OpenAPI 3.0. It's hosted in GCP Cloud. The FastAPI is auto-documented (Swagger UI), and it's exposed to internet.

Now, I am deploying a GCP API Gateway in front of this service. This is the openapi.yaml that is deployed in GCP API Gateway.

swagger: '2.0'
info:
  title: care-chat
  description: Sample API on API Gateway with a Cloud Run backend
  version: 1.0.0
schemes:
- https
produces:
- application/json
x-google-backend:
  address: https://care-navigation-api-zibxv3ldca-ue.a.run.app
paths:
  "/api/v1/tenants":
    get:
      summary: Tenant
      operationId: getTenants
      responses:
        200:
          description: "Success."
          schema:
            type: string

Basically I have two questions:

  1. Does each route/method that I have in my FasAPI need to be exposed one by one in GCP API Gateway? The FastAPI has a lot of complex objects.
  2. How about the documentation? Should I expose it on GCP API Gateway? And if so, how? It seems counter-intuitive to create a map for each of my FastAPI schemas. If so, I suppose that this should be integrated in my ci/cd pipeline.

Solution

  • As I have shared in the above Comment, this Article explains the step by step for using FastAPI and GCP API Gateway.

    The well known problem is clearly explained in the Article, Bridging the Gap: Converting FastAPI OpenAPI to Swagger 2.0 for GCP API Gateway compatibility which will do the trick.

    The Problem

    Incompatibility issue between the OpenAPI document supported by GCP API Gateway, and the OpenAPI document automatically generated from FastAPI itself. Indeed, GCP API Gateway only supports the OpenAPI version 2.0 specification. On the other side, the OpenAPI doc produced using the .openapi() method, only supports version 3.0.

    This mismatch necessitates a manual conversion of the OpenAPI document to version 2.0 before it can be utilized by API Gateway.

    The Solution: For this will be clearly explained in the Link.

    Also the Article contains the clear explanation for:

    1. Why Use Google Cloud API Gateway for FastAPI Projects?.

    2. How to deploy a FastAPI Project with GCP API Gateway.