> ## Documentation index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Ask it with GET https://upstash.com/docs/ask?q=<keywords or question>. Short keywords rank best.
> Use these to discover all available pages before exploring further.
> Feature requests: https://upstash.com/docs/common/help/feature-requests.md lists what users asked for and how to vote for a request or add one.
> Page rating: `GET https://upstash.com/docs/feedback?path=/docs/redis/tutorials/using_google_cloud_functions&helpful=true` (or `false`) records whether this page helped. An optional `comment` (URL-encoded, at most 500 characters) says what was missing or wrong. It is anonymous and needs no key. Comments hold no code, secrets or personal data.

# Serverless Redis on Google Cloud Run functions

<Card title="GitHub Repository" icon="github" href="https://github.com/upstash/redis-js/tree/main/examples/google-cloud-functions" horizontal>
  You can find the project source code on GitHub.
</Card>

Google Cloud Functions is one of the most popular serverless execution
platforms, now part of Cloud Run as Cloud Run functions. Similar to AWS Lambda
it is stateless, namely you need to access external resources to read or write
your applications state. In this post, we will introduce Redis as a database
for your Google Cloud functions.

This tutorial shows how to build a serverless API with Redis on Cloud Run
functions. The API will simply count the views and show it in JSON format.

### Prerequisites

1. [Create a Google Cloud Project.](https://cloud.google.com/resource-manager/docs/creating-managing-projects)
2. [Enable billing for your project.](https://cloud.google.com/billing/docs/how-to/verify-billing-enabled#console)
3. [Enable the Cloud Run, Cloud Build, Artifact Registry, and Cloud Logging APIs.](https://console.cloud.google.com/flows/enableapi?apiid=run.googleapis.com,cloudbuild.googleapis.com,artifactregistry.googleapis.com,logging.googleapis.com)

### Database Setup

Create a Redis database using [Upstash Console](https://console.upstash.com) or [Upstash CLI](https://github.com/upstash/cli). Copy `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` for the next steps.

### Counter Function Setup & Deploy

1. Go to [Cloud Run](https://console.cloud.google.com/run) in Google Cloud Console.
2. Click **Write a function**.
3. Enter a service name, pick a region, and select a recent **Node.js** runtime.
4. Under **Authentication**, select **Allow public access**.
5. Expand the **Containers, Volumes, Networking, Security** section. In the **Variables & Secrets** tab, add the `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` environment variables with the values from your database.
6. Click **Create**. The console creates the service and opens the inline source editor.
7. Set **Function entry point** to `counter`.
8. Update `index.js`
```js index.js
const { Redis } = require("@upstash/redis");
const functions = require('@google-cloud/functions-framework');

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL,
  token: process.env.UPSTASH_REDIS_REST_TOKEN
});

functions.http('counter', async (req, res) => {
  const count = await redis.incr("counter");
  res.send("Counter:" + count);
});
```
9. Update `package.json` to include `@upstash/redis`.
```json package.json
{
  "dependencies": {
    "@google-cloud/functions-framework": "^5.0.0",
    "@upstash/redis": "^1.38.0"
  }
}
```
10. Click **Save and redeploy**.
11. Visit the URL shown at the top of the service page.
