This simple tutorial demonstrates writing, deploying, and triggering an
[HTTP Cloud Run function](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt).

## Objectives

- Write, deploy, and trigger an [HTTP Cloud Run function](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt).

## Costs


In this document, you use the following billable components of Google Cloud:


- Cloud Run functions


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

## Before you begin

1. If you already have the gcloud CLI installed, update it by running the following command:

```sh
gcloud components update
```
2. Prepare your development environment.

   ### Node.js


   [Go to the Node.js setup guide](https://docs.cloud.google.com/nodejs/docs/setup.md.txt)

   ### Python


   [Go to the Python setup guide](https://docs.cloud.google.com/python/docs/setup.md.txt)

   ### Go


   [Go to the Go setup guide](https://docs.cloud.google.com/go/docs/setup.md.txt)

   ### Java


   [Go to the Java setup guide](https://docs.cloud.google.com/java/docs/setup.md.txt)

   ### Ruby


   [Go to the Ruby setup guide](https://docs.cloud.google.com/ruby/docs/setup.md.txt)

## Preparing the application

1. Clone the sample app repository to your local machine:

   ### Node.js

   ```sh
   git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/archive/main.zip)
   as a zip file and extract it.

   ### Python

   ```sh
   git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/python-docs-samples/archive/main.zip)
   as a zip file and extract it.

   ### Go

   ```sh
   git clone https://github.com/GoogleCloudPlatform/golang-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/golang-samples/archive/main.zip)
   as a zip file and extract it.

   ### Java

   ```sh
   git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/java-docs-samples/archive/main.zip)
   as a zip file and extract it.

   ### Ruby

   ```sh
   git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
   ```


   Alternatively, you can
   [download the sample](https://github.com/GoogleCloudPlatform/ruby-docs-samples/archive/main.zip)
   as a zip file and extract it.
2. Change to the directory that contains the Cloud Run functions sample
   code:


   ### Node.js

   ```sh
   cd nodejs-docs-samples/functions/helloworld/
   ```

   ### Python

   ```sh
   cd python-docs-samples/functions/helloworld/
   ```

   ### Go

   ```sh
   cd golang-samples/functions/helloworld/
   ```

   ### Java

   ```sh
   cd java-docs-samples/functions/helloworld/helloworld/
   ```

   ### Ruby

   ```sh
   cd ruby-docs-samples/functions/helloworld/
   ```

   <br />

3. Take a look at the sample code:


   ### Node.js

   ```javascript
   const functions = require('@google-cloud/functions-framework');

   // Register an HTTP function with the Functions Framework that will be executed
   // when you make an HTTP request to the deployed function's endpoint.
   functions.http('helloGET', (req, res) => {
     res.send('Hello World!');
   });
   ```

   ### Python

   ```python
   import functions_framework

   @functions_framework.http
   def hello_get(request):
       """HTTP Cloud Function.
       Args:
           request (flask.Request): The request object.
           <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
       Returns:
           The response text, or any set of values that can be turned into a
           Response object using `make_response`
           <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
       Note:
           For more information on how Flask integrates with Cloud
           Functions, see the `Writing HTTP functions` page.
           <https://cloud.google.com/functions/docs/writing/http#http_frameworks>
       """
       return "Hello World!"

   ```

   ### Go

   ```go

   // Package helloworld provides a set of Cloud Functions samples.
   package helloworld

   import (
   	"fmt"
   	"net/http"
   )

   // HelloGet is an HTTP Cloud Function.
   func HelloGet(w http.ResponseWriter, r *http.Request) {
   	fmt.Fprint(w, "Hello, World!")
   }
   ```

   ### Java

   ```java

   package functions;

   import com.google.cloud.functions.HttpFunction;
   import com.google.cloud.functions.HttpRequest;
   import com.google.cloud.functions.HttpResponse;
   import java.io.BufferedWriter;
   import java.io.IOException;

   public class HelloWorld implements HttpFunction {
     // Simple function to return "Hello World"
     @Override
     public void service(HttpRequest request, HttpResponse response)
         throws IOException {
       BufferedWriter writer = response.getWriter();
       writer.write("Hello World!");
     }
   }
   ```

   ### Ruby

   ```ruby
   require "functions_framework"

   FunctionsFramework.http "hello_get" do |_request|
     # The request parameter is a Rack::Request object.
     # See https://www.rubydoc.info/gems/rack/Rack/Request

     # Return the response body as a string.
     # You can also return a Rack::Response object, a Rack response array, or
     # a hash which will be JSON-encoded into a response.
     "Hello World!"
   end
   ```

   <br />

## Deploying the function

To deploy the function with an HTTP trigger, run the following command in the
directory that contains the sample code (or in the case of Java, the `pom.xml`
file):

### Node.js

```sh
gcloud functions deploy helloGET \
--runtime nodejs22 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Node.js version](https://docs.cloud.google.com/static/functions/docs/runtime-support#node.js) to run
your function.


### Python

```sh
gcloud functions deploy hello_get \
--runtime python312 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Python version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#python) to run
your function.


### Go

```sh
gcloud functions deploy HelloGet \
--runtime go121 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Go version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#go) to run
your function.


### Java

```sh
gcloud functions deploy java-http-function \
--entry-point functions.HelloWorld \
--runtime java17 \
--memory 512MB --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Java version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#java) to run
your function.


### Ruby

```bash
gcloud functions deploy hello_get --runtime ruby33 --trigger-http
```


Use the [`--runtime`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--runtime)
flag to specify the runtime ID of a
[supported Ruby version](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#ruby) to run
your function.


You can optionally use the `--allow-unauthenticated` flag to reach the function
[without authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/managing-access-iam.md.txt#allowing_unauthenticated_http_function_invocation).
This is useful for testing, but we don't recommend using this setting in
production unless you are creating a public API or website. Further, it might
not work for you, depending on your corporate policy settings. See
[Authenticating for invocation](https://docs.cloud.google.com/functions/1stgendocs/securing/authenticating.md.txt) for
details on how to invoke a function that requires authentication.

## Triggering the function

To make an HTTP request to your function, run the following command:

### Node.js

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/helloGET" 
```

### Python

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get" 
```

### Go

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/HelloGet" 
```

### Java

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/java-http-function" 
```

### Ruby

```sh
curl "https://REGION-PROJECT_ID.cloudfunctions.net/hello_get" 
```

where

- `REGION` is the region where your function is deployed. This is visible in your terminal when your function finishes deploying.
- `PROJECT_ID` is your Cloud project ID. This is visible in your terminal when your function finishes deploying.

You can also visit the deployed function's endpoint in your browser to see the
"Hello World!" message.

## Clean up


To avoid incurring charges to your Google Cloud account for the resources used in this
tutorial, either delete the project that contains the resources, or keep the project and
delete the individual resources.

### Deleting the project


The easiest way to eliminate billing is to delete the project that you
created for the tutorial.

To delete the project:

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
2. In the project list, select the project that you want to delete, and then click **Delete**.
3. In the dialog, type the project ID, and then click **Shut down** to delete the project.

<br />

<br />

### Deleting the function

Deleting Cloud Run functions does not remove any resources stored in Cloud Storage.

To delete the Cloud Run function you created in this tutorial,
run the following command:

### Node.js

```sh
gcloud functions delete helloGET 
```

### Python

```sh
gcloud functions delete hello_get 
```

### Go

```sh
gcloud functions delete HelloGet 
```

### Java

```sh
gcloud functions delete java-http-function 
```

### Ruby

```sh
gcloud functions delete hello_get 
```

You can also delete Cloud Run functions from the [Google Cloud console](https://console.cloud.google.com/functions/list).