Skip to content

Cloud Functions

firekitCallable wraps a Cloud Function in a typed, reactive caller. Pass the function name as registered in Firebase.

import { firekitCallable } from 'svelte-firekit';
// Type parameters: <InputType, OutputType>
const sendWelcome = firekitCallable<{ userId: string }, { sent: boolean }>('sendWelcomeEmail');
// Call the function
const result = await sendWelcome.call({ userId: 'abc123' });
console.log(result.sent); // true
PropertyTypeDescription
loadingbooleantrue while the call is in flight
errorError | nullSet if the function call fails
resultOutputType | nullThe resolved return value of the last call
<script lang="ts">
import { firekitCallable } from 'svelte-firekit';
const invite = firekitCallable<{ email: string }, { success: boolean }>('inviteUser');
let email = $state('');
async function handleInvite() {
await invite.call({ email });
}
</script>
<input bind:value={email} type="email" placeholder="[email protected]" />
<button onclick={handleInvite} disabled={invite.loading}>
{invite.loading ? 'Sending…' : 'Send Invite'}
</button>
{#if invite.error}
<p class="error">{invite.error.message}</p>
{/if}
{#if invite.result?.success}
<p>Invite sent!</p>
{/if}

Use firekitCallableFromURL when you need to call a function at a specific endpoint (e.g. a different region, or a non-default Firebase project).

import { firekitCallableFromURL } from 'svelte-firekit';
const fn = firekitCallableFromURL<{ message: string }, { reply: string }>(
'https://us-east1-my-project.cloudfunctions.net/myFunction'
);
const result = await fn.call({ message: 'hello' });

firekitCallableFromURL exposes the same loading, error, and result properties as firekitCallable.

Firebase Functions errors include a code and details field. Cast to FunctionsError for typed access:

import type { FunctionsError } from 'firebase/functions';
try {
await myFn.call(input);
} catch (err) {
const fnErr = err as FunctionsError;
console.log(fnErr.code); // e.g. 'functions/permission-denied'
console.log(fnErr.details); // custom error payload from the function
}