Dart Functions

Write serverless functions in Dart — the same language as your Flutter app. Deploy and invoke from the dashboard or directly from the SDK.

Available runtimes

deno

TypeScript / JavaScript

Default runtime. Full Deno 2 API, npm compatible.

dart

Dart

Flutter-native runtime. Write functions in the same language as your app.

Writing a Dart function

A Dart function must export an async handler function that accepts a context map and returns a map.

Future<Map<String, dynamic>> handler(Map<String, dynamic> ctx) async {
  // Access request body
  final body = ctx['request']['body'] as Map<String, dynamic>? ?? {};
  final name = body['name'] as String? ?? 'World';

  // Access database
  final db = ctx['db'] as Map<String, dynamic>;
  final records = await (db['find'] as Function)('users', {'active': true});

  return {
    'message': 'Hello, $name!',
    'user_count': (records['records'] as List?)?.length ?? 0,
  };
}

Function signature

The handler must be a top-level function named handler with the signature Future<Map<String, dynamic>> handler(Map<String, dynamic> ctx).

Context object

The ctx map contains everything your function needs:

Future<Map<String, dynamic>> handler(Map<String, dynamic> ctx) async {
  // Request data
  final request = ctx['request'] as Map<String, dynamic>;
  final body = request['body'] as Map<String, dynamic>? ?? {};
  final headers = request['headers'] as Map<String, dynamic>? ?? {};

  // Database helpers
  final db = ctx['db'] as Map<String, dynamic>;

  // Insert a record
  final record = await (db['insert'] as Function)('posts', {
    'title': 'Hello from Dart',
    'published': true,
  });

  // Query records
  final posts = await (db['find'] as Function)('posts', {'published': true}, 10);

  // Update a record
  await (db['update'] as Function)(record['id'], {'title': 'Updated'});

  // Delete a record
  await (db['delete'] as Function)(record['id']);

  return {'ok': true};
}

Deploy from dashboard

Go to your project in app.koolbase.com → Functions → New Function. Select Dart as the runtime, paste your code, and click Deploy.

Deploy from Flutter SDK

Deploy functions programmatically using the Flutter SDK. Requires an authenticated user session.

lib/deploy_function.dartDart
await Koolbase.functions.deploy(
  projectId: 'your-project-id',
  name: 'send-welcome-email',
  runtime: FunctionRuntime.dart,
  timeoutMs: 15000,
  code: r'''
    Future<Map<String, dynamic>> handler(Map<String, dynamic> ctx) async {
      final body = ctx['request']['body'] as Map<String, dynamic>? ?? {};
      final email = body['email'] as String?;

      // Send email logic here

      return {'sent': true, 'to': email};
    }
  ''',
);

Invoke from Flutter

Invoke any function regardless of runtime — the invoke() API is the same for both Deno and Dart functions.

lib/home_screen.dartDart
final result = await Koolbase.functions.invoke(
  'send-welcome-email',
  body: {
    'email': 'user@example.com',
    'name': 'Kennedy',
  },
);

if (result.success) {
  print(result.data); // {'sent': true, 'to': 'user@example.com'}
}

Invoke via REST API

curl -X POST https://api.koolbase.com/v1/sdk/functions/send-welcome-email \
  -H "Content-Type: application/json" \
  -H "x-api-key: pk_live_your_key" \
  -d '{"body": {"email": "user@example.com"}}'

Limits

Max execution time30 seconds
Max code size1 MB
Dart SDK version3.11.4
Network accessAllowed (HTTP/HTTPS)
File system accessNot allowed
DB triggersSupported — same as Deno functions