Setup Cloud Functions
The functions module integrates Cloud Functions in your project. It provides a serverless framework that lets you automatically run backend code in response to events triggered by background events, HTTPS requests, the Admin SDK, or Cloud Scheduler jobs.
Setup
-
Open a terminal into the functions folder and run:
-
Then deploy the functions with
All functions should be now ready to be used. Depending on which modules you included, you might have different functions.
Testing
To build and test your functions you can run:
This command will build the TypeScript files and watch for changes. At the same time it will start the Firebase Emulators that will automatically update the functions when changes happen.
By default it only starts the Functions emulator but you can modify the command under
package.json
> scripts
> serve
to start other emulators.
To make the emulator work with other Firebase services you can either start those as emulator or
use a service_account.
More information at the official docs: https://firebase.google.com/docs/emulator-suite
The basics
To learn how to use Cloud Functions, we recommend checking the official documentation, in this section we will only cover ShipFlutter specifics.
Directorylib/modules/functions
Directorysrc
- index.ts
- main.ts
- package.json
The package.json
is used to define the JavaScript dependencies and other configuration.
The index.ts
is the main entry point required to deploy the functions. It collects all the functions and exports them.
It also initializes the App and the Express server defined in main.ts
.
The main.ts
configures the express server to enable CRUD interfaces for HTTP functions.
For example, we use this POST
webhook for handling Lemon Squeezy payments in the payments module.
Callable Functions
A more common approach for using functions in an app, is to use the callable functions. Those are functions that can only be called with the Firebase SDK with the main benefit that automatically handles, Firebase Authentication tokens, FCM tokens, and App Check tokens, when available. In addition, the trigger automatically deserializes the request body and validates auth tokens.
To use a callable function, use the functionsService
instance in app_functions.dart
: