Invoke Functions from Flutter

Call your deployed Koolbase Functions directly from your Flutter app using Koolbase.functions.invoke().

How it works

The functions client sends an authenticated HTTP POST request to your deployed function using the environment public key. The function runs on the server in the Deno runtime and returns a JSON response which the SDK parses automatically.

Authentication

The SDK automatically includes your environment's x-api-key header on every invocation. You don't need to manage tokens manually.

Basic usage

dart
final result = await Koolbase.functions.invoke('send-welcome-email');

if (result.success) {
  print(result.data); // parsed JSON response
}

Passing a body

Pass any JSON-serializable map as the request body. Your function receives it via ctx.request.

dart
final result = await Koolbase.functions.invoke(
  'process-order',
  body: {
    'order_id': 'ord_123',
    'user_id': Koolbase.auth.currentUser?.id,
    'amount': 4999,
  },
);

if (result.success) {
  final orderId = result.data?['order_id'];
  print('Order processed: $orderId');
}

Error handling

invoke() throws a FunctionInvokeException if the request fails or the function returns a non-2xx status code.

dart
try {
  final result = await Koolbase.functions.invoke(
    'send-welcome-email',
    body: {'email': 'user@example.com'},
  );
  print('Function result: ${result.data}');
} on FunctionInvokeException catch (e) {
  print('Error: ${e.message}');
  print('Status: ${e.statusCode}');
} catch (e) {
  print('Unexpected error: $e');
}

FunctionInvokeException

Thrown when the HTTP request fails (network error) or when the function returns a non-2xx status code. The message field contains the error string from the function response, and statusCode is the HTTP status if available.

Custom timeout

The default timeout is 30 seconds. Override it per call for long-running functions.

dart
final result = await Koolbase.functions.invoke(
  'generate-report',
  body: {'period': 'monthly'},
  timeout: const Duration(seconds: 60),
);

FunctionInvokeResult

FieldTypeDescription
statusCodeintHTTP status code returned by the function.
dataMap<String, dynamic>?Parsed JSON response body, or null if the response is not JSON.
rawStringRaw response body as a string.
successboolTrue if statusCode is 200–299.

Example function (server-side)

The matching function deployed in the Koolbase dashboard:

typescript
export async function handler(ctx) {
  const { email, userId } = ctx.request.body

  if (!email) {
    return { error: 'email is required' }
  }

  // Insert a record, call an external API, send an email, etc.
  await ctx.db.insert('notifications', {
    user_id: userId,
    type: 'welcome',
    sent_at: new Date().toISOString(),
  })

  return { ok: true, message: 'Welcome email queued' }
}

API reference

MethodDescription
Koolbase.functions.invoke(name)Invoke a function by name. Returns FunctionInvokeResult.
Koolbase.functions.invoke(name, body: {...})Invoke with a JSON body payload.
Koolbase.functions.invoke(name, timeout: Duration(...))Invoke with a custom timeout.