Google Cloud Functions Client Library

1.23.0 · active · verified Sun Apr 12

The `google-cloud-functions` Python client library provides a programmatic interface for managing Google Cloud Functions. Cloud Functions is a scalable, pay-as-you-go Functions-as-a-Service (FaaS) offering that allows you to run event-driven code without managing servers. This library is part of the larger `google-cloud-python` monorepo, with its latest version being 1.23.0, and it requires Python 3.9 or higher. While the monorepo sees frequent updates, this specific client library updates less frequently. [3, 19, 25]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to write a simple HTTP-triggered Google Cloud Function using the `functions-framework` library, which is the standard for defining and running Python Cloud Functions. To deploy, save the code as `main.py` and use the `gcloud functions deploy` command. For local testing, install `functions-framework` and run it against your function. [4, 8, 11]

import functions_framework
import os

@functions_framework.http
def hello_http(request):
    """HTTP Cloud Function that responds to HTTP requests.
    Args:
        request (flask.Request): The request object.
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`.
    """
    request_json = request.get_json(silent=True)
    request_args = request.args

    # Example of accessing an environment variable set during deployment
    target_name = os.environ.get('FUNCTION_TARGET_NAME', 'World')

    name = target_name
    if request_json and 'name' in request_json:
        name = request_json['name']
    elif request_args and 'name' in request_args:
        name = request_args['name']

    return f'Hello {name}!'

# To deploy this function (save as main.py):
# gcloud functions deploy hello-http --gen2 --runtime=python312 \
# --region=us-central1 --source=. --entry-point=hello_http --trigger-http \
# --set-env-vars=FUNCTION_TARGET_NAME=Pythonista

# To test locally after `pip install functions-framework`:
# functions-framework --target hello_http --debug
# Then send a request to http://localhost:8080/

view raw JSON →