This example demonstrates the core functionality of Firebase Functions for Dart.
-
onRequest - Raw HTTP handler
- Endpoint:
/helloWorld - Returns plain text response
- Endpoint:
-
onCall - Callable function (untyped)
- Endpoint:
/greet - Accepts JSON:
{"data": {"name": "Alice"}} - Returns:
{"result": {"message": "Hello Alice!"}}
- Endpoint:
-
onCall with Streaming - Server-Sent Events
- Endpoint:
/streamNumbers - Streams 5 numbers with delays
- Set
Accept: text/event-streamheader
- Endpoint:
- onMessagePublished - Pub/Sub message handler
- Triggered by messages to
my-topic - Logs message details to console
- Triggered by messages to
dart pub upgradedart run bin/server.dartThe server will start on http://localhost:8080.
# onRequest
curl http://localhost:8080/helloWorld
# onCall
curl -X POST http://localhost:8080/greet \
-H "Content-Type: application/json" \
-d '{"data": {"name": "Alice"}}'
# onCall with streaming
curl -X POST http://localhost:8080/streamNumbers \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{"data": {}}'Send a CloudEvent POST request:
curl -X POST http://localhost:8080/onMessagePublished_my-topic \
-H "Content-Type: application/json" \
-d '{
"specversion": "1.0",
"type": "google.cloud.pubsub.topic.v1.messagePublished",
"source": "//pubsub.googleapis.com/projects/my-project/topics/my-topic",
"id": "test-123",
"time": "2024-01-01T12:00:00Z",
"data": {
"message": {
"data": "SGVsbG8gV29ybGQh",
"attributes": {"key": "value"},
"messageId": "123456",
"publishTime": "2024-01-01T12:00:00Z"
},
"subscription": "projects/my-project/subscriptions/my-sub"
}
}'Note: The data SGVsbG8gV29ybGQh is base64-encoded "Hello World!"
firebase emulators:start --only functionsThe emulator will automatically detect the Dart runtime and start your functions.