Invoke Functions from Your App

Call your deployed Koolbase Functions directly from your 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 sends your environment's public key on every invocation, and the signed-in person's session when there is one, so the function knows who called it. Nothing to pass.

Basic usage

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.

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

print('Order processed: ${result.data?['order_id']}');

Error handling

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

try {
  final result = await Koolbase.functions.invoke(
    'send-welcome-email',
    body: {'email': '[email protected]'},
  );
  print('Function result: ${result.data}');
} on FunctionTimeoutException {
  print('The function took too long.');
} on FunctionInvokeException catch (e) {
  print('Error: ${e.message} (status ${e.statusCode})');
}

FunctionInvokeException / FunctionInvokeError

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

In Flutter the default timeout is 30 seconds. Override it per call for long-running functions.

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:

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.