Text of docs.cloud.google.com › run › docs › write-functions as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Write Cloud Run functions | Google Cloud Documentation

Skip to main content Write Cloud Run functions Stay organized with collections Save and categorize content based on your preferences. The Functions Framework lets you write Cloud Run functions that respond to HTTP requests or process cloud events. You can implement HTTP handlers for webhooks and URL endpoints, or event-driven handlers triggered by services like Pub/Sub and Cloud Storage. Functions Framework overview When writing functions source code, you must use the Functions Framework , an open source library for writing Cloud Run functions.

With the Functions Framework, you can write lightweight functions that run in Cloud Run and in other environments, including your local development machine and Knative-based environments. The Functions Framework lets you: Invoke a Cloud Run function in response to a request. Automatically unmarshal events conforming to the CloudEvents specification, an industry-standard specification for describing event data in a common way.

Start a local development server for testing. The Functions Framework provides an interface for building modular services. To use the Functions Framework in your source code, specify the following: Function entry point Signature type Function entry point Your source code must define a function entry point, which is the code that runs when Cloud Run invokes your function. You specify this entry point when you deploy your function.

How you define the entry point depends on the language runtime you use. Some languages use a function as their entry point, while others use a class. Signature Type When you write the source code of a function with the Functions Framework, you must specify one of the two signature types: HTTP functions : Registers an HTTP handler function. Use an HTTP function when your function needs a URL endpoint and must respond to HTTP requests, such as for webhooks.

Event-driven functions , also known as CloudEvents functions : Registers a CloudEvents handler function. Use an event-driven function when your function is triggered directly in response to events within your Google Cloud project, such as messages on a Pub/Sub topic or changes in a Cloud Storage bucket. Source directory structure The Functions Framework is supported in a number of programming languages. The language runtime you choose and the type of function you want to write determines how to structure your code and implement your function.

For Cloud Run to locate your function definition, each language runtime has requirements for structuring your source code. Node.js The basic directory structure for Node.js functions is as follows: . ├── index.js └── package.json By default, Cloud Run attempts to load source code from a file named index.js at the root of your function directory. To specify a different main source file, use the main field in your package.json file.

Your package.json file must also include the Functions Framework for Node.js as a dependency: { "main" : "index.js" , "dependencies" : { "@google-cloud/functions-framework" : "^3.0.0" }, "type" : "module" } The code in your main file must define your function entry point and can import other code and Node.js modules. The main file can also define multiple function entry points that can be deployed separately. See the Node.js runtime overview and the Functions Framework for Node.js for more details.

Python The basic directory structure for Python functions is as follows: . ├── main.py └── requirements.txt Cloud Run loads source code from a file named main.py at the root of your function directory. You must name your main file main.py . Your requirements.txt file must include the Functions Framework for Python as a dependency: functions - framework == 3. * The code in your main.py file must define your function entry point and can import other code and external dependencies as normal.

The main.py file can also define multiple function entry points that can be deployed separately. See the Python runtime overview and the Functions Framework for Python for more details. Go The basic directory structure for Go functions is as follows: . ├── myfunction.go └── go.mod Your function must be in a Go package at the root of your project. The package and its source files can have any name, except your function cannot be in package main .

If you need a main package, for example for local testing, you can create one in a subdirectory: . ├── myfunction.go ├── go.mod └── cmd/ └── main.go Your go.mod file must include the Functions Framework for Go as a dependency: module example . com / my - module require ( github . com / GoogleCloudPlatform / functions - framework - go v1 .5.2 ) The code in your root package must define your function entry point and can import other code from subpackages and dependencies as normal.

Your package can also define multiple function entry points that can be deployed separately. See the Go runtime overview and the Functions Framework for Go for more details. Java The basic directory structure for Java functions is as follows: . ├── pom.xml └── src/ └── main/ └── java/ └── MyFunction.java Your Java source files must be under the src/main/java/ directory and can have any name.

If your source files declare a package, add an extra directory under src/main/java with the name of the package: . ├── pom.xml └── src/ └── main/ └── java/ └── mypackage/ └── MyFunction.java We recommend putting associated tests under a src/test/java/ subdirectory. Your pom.xml file must include the Functions Framework for Java as a dependency: ... <dependency> <groupId>com.google.cloud.functions</groupId> <artifactId>functions-framework-api</artifactId> <version>1.0.4</version> </dependency> ...

The code in your source files must define your function entry point and can import other code and external dependencies as normal. Your source files can also define multiple function entry points that can be deployed separately. See the Java runtime overview and the Functions Framework for Java for more details. .NET The basic directory structure for .NET functions is as follows: . ├── MyFunction.cs └── MyProject.csproj You can structure your projects as you would any other .NET source code.

Your source files can have any name. Your project file must include the Functions Framework for .NET as a dependency: ... <PackageReference Include="Google.Cloud.Functions.Hosting" Version="1.0.0" /> ... The code in your source files must define your function entry point and can import other code and external dependencies as normal. Your source files can also define multiple function entry points that can be deployed separately.

See the .NET runtime overview and the Functions Framework for .NET for more details. Ruby The basic directory structure for Ruby functions is as follows: . ├── app.rb ├── Gemfile └── Gemfile.lock Cloud Run loads source code from a file named app.rb at the root of your function directory. Your main file must be named app.rb . Your Gemfile file must include the Functions Framework for Ruby as a dependency: source "https://rubygems.org" gem "functions_framework", "~> 1.0" The code in your app.rb file must define your function entry point and can import other code and external dependencies as normal.

The app.rb file can also define multiple function entry points that can be deployed separately. See the Ruby runtime overview and the Functions Framework for Ruby for more details. PHP The basic directory structure for PHP functions is as follows: . ├── index.php └── composer.json Cloud Run loads source code from a file named index.php at the root of your function directory. You must name your main file index.php .

Your composer.json file must include the Functions Framework for PHP as a dependency: { "require" : { "google/cloud-functions-framework" : "^1.1" } } The code in your index.php file must def

The rest of this page is on the live site.