Prompt
How do I deploy an HTTP API with Google Cloud Functions?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
To deploy an HTTP API with Google Cloud Functions, you typically create a function that handles HTTP requests and then deploy it as an HTTP-triggered function.
1) Prerequisites
- A Google Cloud project
- Billing enabled
gcloudCLI installed and authenticated
gcloud auth login
gcloud config set project PROJECT_ID
Also enable the Cloud Functions API if needed:
gcloud services enable cloudfunctions.googleapis.com
2) Write your function
Node.js example
Create index.js:
exports.api = (req, res) => {
if (req.method === 'GET') {
res.status(200).json({ message: 'Hello from Cloud Functions!' });
} else {
res.status(405).send('Method Not Allowed');
}
};
Create package.json:
{
"name": "http-api",
"version": "1.0.0",
"main": "index.js",
"dependencies": {}
}
3) Deploy the function
Deploy as an HTTP function and allow unauthenticated access if you want public API access:
gcloud functions deploy api \
--runtime nodejs20 \
--trigger-http \
--allow-unauthenticated \
--entry-point api
After deployment, gcloud will output a URL for your API.
4) Test it
curl https://REGION-PROJECT_ID.cloudfunctions.net/api
You should get:
{"message":"Hello from Cloud Functions!"}
5) Add routes/logic for a real API
Cloud Functions is a single handler, so you usually implement routing inside the function:
exports.api = (req, res) => {
const { method, path } = req;
if (method === 'GET' && path === '/users') {
res.json([{ id: 1, name: 'Alice' }]);
} else if (method === 'POST' && path === '/users') {
res.status(201).json({ status: 'created' });
} else {
res.status(404).send('Not found');
}
};
6) Optional: Use Cloud Functions with API Gateway
If you need:
- API keys
- Auth policies
- Rate limiting
- Better API management
put API Gateway in front of Cloud Functions.
Notes
- Cloud Functions is best for lightweight APIs and event-driven workloads.
- For more complex HTTP APIs, consider Cloud Run or API Gateway + Cloud Run.
- In newer Google Cloud setups, Cloud Functions (2nd gen) is often preferred.
If you want, I can also show you:
- a Python version,
- a 2nd gen deployment, or
- how to expose multiple routes in one function.