World Kindness Day Snap Cloud Setup
Create the Snap Cloud tables, trigger, and RPC functions for the World Kindness Day sample, which connects a SPECS Lens to a companion web app.
- Snapchat: not supported
- SPECS: fully supported
- Camera Kit Web: not supported
- Camera Kit Mobile: not supported
- SnapchatNot supported
- SPECSFully supported
- Camera Kit WebNot supported
- Camera Kit MobileNot supported
This guide sets up your own Snap Cloud backend so the World Kindness Day Lens and web app can read and write shared data.
The World Kindness Day Lens and the World Kindness Day web app share this backend. You create the Snap Cloud project, tables, and functions once, and both clients use them.
Why the World Kindness Day sample needs Snap Cloud
Most Lenses run entirely on the device and need no backend. This project, though, must store and share data across all its users:
- Whether a user has already pledged.
- The global total number of pledges.
- The balloon counts shown in both the Lens and the web app.
That's what Snap Cloud provides, powered by Supabase, for the database, authentication, and server-side logic:
- A database that stores the pledge records.
- RPC functions (SQL functions) that update and return the totals securely.
- Authentication, so each SPECS user has a unique
user_id. - Realtime events, which let the web app update the counter instantly.
The Lens and the web app both read from and write to this backend.
Where the World Kindness Day setup steps happen
- In the Snap Cloud dashboard: all the database setup. You create the tables, add the functions (RPCs), set permissions, run queries, and optionally insert test data.
- In Lens Studio: you import the project's credentials from the Supabase panel, and the Lens scripts call the RPCs, such as
pledge_and_total_onceandget_kindness_total_all. - In the web app: the app connects with your project's URL and anon (public) API key, and calls RPCs such as
get_kindness_total_allto show the live global pledge total.
Once the backend is set up, the Lens and the web app work together.
Create the Snap Cloud project
Create your project and import its credentials as described in Get Started.
Create the tables, trigger, and RPC functions in the SQL Editor
In the Snap Cloud dashboard, open SQL Editor from the left sidebar, and run each block below in order.

This SQL grants the anon and authenticated roles direct access to both tables and turns on no row level security. Because the web app's anon key is public, anyone who has it can insert pledges and change totals through the REST API. Before you publish your own version, restrict writes to the RPC functions.
1. Create the kindness_pledges table
This table stores who has pledged, one row per user. Making user_id the PRIMARY KEY ensures each user appears only once, and the RPC uses ON CONFLICT DO NOTHING, so repeat pledges are ignored.
create table public.kindness_pledges (
user_id uuid primary key,
pledged_at timestamptz default now()
);
grant select, insert on public.kindness_pledges to anon, authenticated;
2. Create the kindness_totals table
This table stores the daily pledge totals that the RPC functions read.
create table public.kindness_totals (
pledge_date date primary key,
total bigint default 0
);
grant select, insert, update on public.kindness_totals to anon, authenticated;
3. Create the trigger that increments the daily total
The trigger trg_bump_total fires only when a pledge row is created. It runs the function bump_total_after_pledge, which increments today's total in the database.
create or replace function public.bump_total_after_pledge()
returns trigger
language plpgsql
as $$
begin
insert into public.kindness_totals (pledge_date, total)
values (current_date, 1)
on conflict (pledge_date)
do update set total = public.kindness_totals.total + 1;
return new;
end;
$$;
drop trigger if exists trg_bump_total on public.kindness_pledges;
create trigger trg_bump_total
after insert on public.kindness_pledges
for each row execute procedure bump_total_after_pledge();
4. Create the pledge_and_total_once RPC
Inserts the user's pledge only if they haven't pledged before, and returns the all-time pledge total.
create or replace function public.pledge_and_total_once()
returns bigint
language plpgsql
as $$
declare
t bigint := 0;
begin
insert into public.kindness_pledges (user_id)
values (auth.uid())
on conflict (user_id) do nothing;
select coalesce(sum(total), 0) into t
from public.kindness_totals;
return t;
end;
$$;
grant execute on function public.pledge_and_total_once() to anon, authenticated;
5. Create the get_kindness_total_all RPC
Returns the total number of pledges across all days.
create or replace function public.get_kindness_total_all()
returns bigint
language sql
stable
as $$
select coalesce(sum(total), 0)
from public.kindness_totals;
$$;
grant execute on function public.get_kindness_total_all() to anon, authenticated;
6. Create the has_pledged_ever RPC
Checks whether the current user has already pledged.
create or replace function public.has_pledged_ever()
returns boolean
language sql
as $$
select exists(
select 1
from public.kindness_pledges
where user_id = auth.uid()
);
$$;
grant execute on function public.has_pledged_ever() to anon, authenticated;
Get the project keys for the web app
The web app needs your project URL and its anon public key to read the pledge total. Where to find them and how to enter them in the app's config.js is in World Kindness Day Web App Setup.
Add test data to check the World Kindness Day backend
Before you run the web app or open the Lens, insert a sample pledge to check that your setup works. In the SQL Editor, run:
insert into public.kindness_pledges (user_id)
values ('11111111-1111-1111-1111-111111111111');
The table has no foreign key to auth.users, so a made-up user_id works.
-
A row is added to
kindness_pledges.
-
The trigger
trg_bump_totalrunsbump_total_after_pledge, which increments today's count inkindness_totals.
Related
- World Kindness Day Lens Setup: connect the Lens to this backend.
- World Kindness Day Web App Setup: connect the web app to this backend.
- Databases: tables and row level security in Snap Cloud.