Skip to content

Cloud Functions

The functions module integrates Firebase Cloud Functions in your project. It provides a serverless framework to run backend code in response to events triggered by HTTPS requests, background events, or scheduled jobs.

  • Directoryfunctions/src/
    • Directoryaccount/ // Account management
    • Directorypayment/ // Payment processing
    • Directorychat/ // Chat functionality
    • Directorygenkit/ // AI features (optional)
    • index.ts // Function exports
    • main.ts // Express app setup
    • store.ts // Firestore helpers

Setup

From within the functions folder:

  1. Install dependencies:

    Terminal window
    npm install
  2. Create a .env file in the functions folder:

    Terminal window
    # Required for Lemon Squeezy payment module
    KEY_LEMON_SQUEEZY_API=xxx
  3. Modify and adapt for your needs

  4. Deploy the functions:

    Terminal window
    firebase deploy --only functions

Express Engine

ShipFlutter uses Express.js to handle HTTP functions. The setup is defined in main.ts and it means all HTTP endpoints:

  1. Are prefixed with /v1 for versioning
  2. Accept JSON payloads automatically
  3. Follow the pattern: https://REGION-PROJECT_ID.cloudfunctions.net/api/v1/PATH
import express from "express";
import bodyParser from "body-parser";
const app = express();
const main = express();
// All routes are prefixed with /v1
main.use("/v1", app);
main.use(bodyParser.json());
// Example CRUD endpoint
// URL: https://REGION-PROJECT_ID.cloudfunctions.net/api/v1/hello
app.post("/hello", (req, res) => {
// Handle webhook
res.status(200).send("OK");
});

Function Types

ShipFlutter uses several types of Cloud Functions:

// Express API endpoint
app.post("/payment/create", async (req, res) => {
// Handle webhook
res.status(200).send("OK");
});

Used for webhooks and external integrations. For example:

https://REGION-PROJECT.cloudfunctions.net/api/v1/payment/event/ls

Testing Functions

Test your functions locally using the Firebase Emulator:

Terminal window
cd functions
npm run serve

Available Modules

ShipFlutter includes several function modules:

  1. Account Management (/account)

    • User data synchronization
    • Account deletion
    • Social auth
  2. Payment Processing (/payment)

    • RevenueCat webhooks
    • LemonSqueezy webhooks
    • Product syncing
  3. Chat Features (/chat)

    • Message handling
    • Chat history
    • Real-time updates
  4. GenKit (/genkit)

    • AI-powered features
    • Vertex AI and Gemini integration
    • Custom flows

Using Functions in Flutter

The AppFunctions class provides a type-safe way to call Cloud Functions:

// Call a function with custom payload
final result = await functionsService.instance.call(
'functionName',
payload: {'key': 'value'},
parser: (data) => MyModel.fromJson(data),
);
// Simple function call
final greeting = await functionsService.instance.call<String>(
'hello',
parser: (data) => data as String,
);