> ## Documentation Index
> Fetch the complete documentation index at: https://upstash-dx-2808-improve-quickstarts.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Serverless Redis on Google Cloud 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 theme={null}
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 theme={null}
{
  "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.
