Docs
Functions
Creating a function

Creating a function

Please complete Setting up Celest prior to reading this guide.

Create serverless functions with Celest to connect and aggregate information from different parts of your backend, and build custom business logic that runs completely in the cloud. You define your functions as regular Dart functions and Celest takes care of deploying and managing the backend for you.

Building Celest Functions

To get started building your first serverless cloud function, navigate to the <flutter_app>/celest/functions/ folder and create a file named <api_name>.dart. You can create as many APIs as you want in this directory. Think of each file as a way to organize and group multiple Celest Functions of similar functionality into a namespace.

Celest Functions are defined as top-level functions as shown below.

Future<String> sayHello(String name) async {
  return 'Hello, $name';
}
 
Future<String> sayGoodbye(String name) async {
  return 'Goodbye, $name';
}

That's all you need to define your API! Go to your console and run the following command.

celest start

A local environment will spin up for you and watch for changes to your Celest Functions. A client is also generated for your which you can import in your Flutter app to automatically connect to your Celest Functions.

Connecting to your Celest Functions

The following is an example of how you would use the Celest client in your main.dart file to call a Celest Function and show the response from the backend.

import 'package:flutter/material.dart';
// Import the generated Celest client
import 'package:celest_backend/client.dart';
 
void main() {
  // Initializes Celest in your Flutter app
  celest.init();
  runApp(const MyApp());
}
 
class MyApp extends StatelessWidget {
  const MyApp({super.key});
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Homepage'),
        ),
        body: Center(
          child: FutureBuilder(
            // Call your function using the Celest client
            future: celest.functions.greeting.sayHello('Celest'),
            builder: (_, snapshot) => switch (snapshot) {
              AsyncSnapshot(:final data?) => Text(data),
              AsyncSnapshot(:final error?) =>
                Text('${error.runtimeType}: $error'),
              _ => const CircularProgressIndicator(),
            },
          ),
        ),
      ),
    );
  }
}

Next steps

You now know how to create Celest Functions and connect them to your Flutter app. We have other guides to help explain how to use features such as logging, using custom data types, and managing environment variables.


Copyright © 2024 Teo, Inc. (Celest)