Environment variables
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates how to use environment variables within a Cloud Function.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content demonstrates how to access and use environment variables within a Cloud Function across multiple programming languages.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples provided illustrate retrieving the value of the "FOO" environment variable in C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample is designed to return either the value of the "FOO" environment variable or a message indicating it is not set.\u003c/p\u003e\n"],["\u003cp\u003eTo interact with Cloud Run functions, the content emphasizes the necessity of setting up Application Default Credentials, with a link to detailed instructions provided.\u003c/p\u003e\n"],["\u003cp\u003eThe cloud sample browser is recommended as a resource to search and filter code samples for other Google Cloud Products.\u003c/p\u003e\n"]]],[],null,["# Environment variables\n\nDemonstrates how to use environment variables within a Cloud Function.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Configure environment variables (1st gen)](/functions/1stgendocs/configuring/env-var)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n using Google.Cloud.Functions.Framework;\n using Microsoft.AspNetCore.Http;\n using System;\n using System.Threading.Tasks;\n\n namespace EnvironmentVariables;\n\n public class Function : IHttpFunction\n {\n public async Task HandleAsync(HttpContext context)\n {\n string foo = Environment.GetEnvironmentVariable(\"FOO\")\n ?? \"Specified environment variable is not set.\";\n await context.Response.WriteAsync(foo, context.RequestAborted);\n }\n }\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package tips contains tips for writing Cloud Functions in Go.\n package tips\n\n import (\n \t\"fmt\"\n \t\"net/http\"\n \t\"os\"\n )\n\n // EnvVar is an example of getting an environment variable in a Go function.\n func EnvVar(w http.ResponseWriter, r *http.Request) {\n \tfmt.Fprintf(w, \"FOO: %q\", os.Getenv(\"FOO\"))\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.functions.HttpFunction;\n import com.google.cloud.functions.HttpRequest;\n import com.google.cloud.functions.HttpResponse;\n import java.io.BufferedWriter;\n import java.io.IOException;\n\n public class EnvVars implements HttpFunction {\n\n // Returns the environment variable \"foo\" set during function deployment.\n @Override\n public void service(HttpRequest request, HttpResponse response)\n throws IOException {\n BufferedWriter writer = response.getWriter();\n String foo = System.getenv(\"FOO\");\n if (foo == null) {\n foo = \"Specified environment variable is not set.\";\n }\n writer.write(foo);\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n exports.envVar = (req, res) =\u003e {\n // Sends 'bar' as response\n res.send(process.env.FOO);\n };\n\n### PHP\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n use Psr\\Http\\Message\\ServerRequestInterface;\n\n function envVar(ServerRequestInterface $request): string\n {\n return getenv('FOO') . PHP_EOL;\n }\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import os\n\n\n def env_vars(request):\n return os.environ.get(\"FOO\", \"Specified environment variable is not set.\")\n\n### Ruby\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"functions_framework\"\n\n FunctionsFramework.http \"env_vars\" do |_request|\n ENV[\"FOO\"] || \"Specified environment variable is not set.\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]