Cloud Function Environment
Complete guide to the execution environment, request handling, and response types in CocoBase cloud functions.Overview
CocoBase cloud functions execute in a sandboxed Python 3.10 environment with access to:- Request object (
requestorreq) - Access to query params, payload, headers, user info - Database service (
db) - Query and manage your data - Template renderer (
render) - Render HTML with Jinja2 - Environment object (
env) - Project and environment info
Function Execution
Execution URL
When you create a cloud function, you get an execution URL:HTTP Methods
GET Request:The request Object
The request object (also available as req) provides access to incoming request data.
Properties
Methods
request.get(key, default=None)
Get value from payload (POST) or query params (GET):
request.json()
Get entire payload as dictionary:
request.send_mail()
Send email using CocoMailer integration:
User Authentication
Access authenticated user data:Response Types
JSON Response (Default)
Return a dictionary to send JSON:HTML Response
Userender.render_html() for HTML pages:
HTML with Template Variables
Status Codes
Return a tuple with status code:Environment Variables
Access environment and project information:Available Python Modules
Cloud functions have access to these Python standard library modules without import statements:| Module | Description | Common Uses |
|---|---|---|
json | JSON encoding/decoding | Parse API responses, serialize data |
datetime | Date and time class | Current time, timestamps |
timedelta | Time duration class | Add/subtract time, expiration |
time | Time utilities | Delays, timestamps, timing |
math | Mathematical functions | Calculations, rounding |
re | Regular expressions | Pattern matching, validation |
secrets | Cryptographically secure random | OTP generation, tokens, passwords |
uuid | UUID generation | Unique identifiers |
hashlib | Hash functions | MD5, SHA256, data integrity |
base64 | Base64 encoding | Encode/decode binary data |
string | String constants | ASCII letters, digits, punctuation |
collections | Data structures | Counter, defaultdict, deque |
Examples
OTP Generation (Secure):Error Handling
Next Steps
- Quick Reference - Cheat sheet for common patterns
- Examples - Real-world code examples
- Main Features - Overview and advanced topics
