Skip to main content

Basic Setup

Run the Snap Cloud Examples package's first examples on SPECS: sign-in, tables, a realtime cursor, storage downloads, and an edge function call.

  • 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

Platform details

  • Snapchat — Not supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Not supported.

  • Camera Kit Mobile — Not supported.

The Get Started panel of the Snap Cloud Examples package holds the examples for the foundational Snap Cloud services: authentication, tables, realtime, storage, and edge functions. These five cover what the more advanced Media and Global Leaderboard examples build on. This page walks through each: what it does, the backend it needs, and the page that explains the code.

Before you start

  1. Set up Snap Cloud: see Get Started, through Import the project credentials.
  2. Install the Snap Cloud Examples package, place its prefab in your scene, and assign your project on [SnapCloudRequirements]: see Examples.

Every example reads the project from that one [SnapCloudRequirements] object, creates its client with heartbeatIntervalMs: 2500, and signs in the user's SPECS Account with signInWithIdToken({ provider: 'snapchat', token: '' }), as in Create the client and sign in.

The four examples on the Get Started panel (Tables, Realtime Cursor, Storage, and Edge Functions) are disabled children of the panel. Enable one at a time; the carousel steps through them. Only the enabled example responds to its panel button.

The prefab ships with instructions in some text inputs instead of working values, such as YOUR BUCKET NAME - TRY "specs-bucket". Replace each one before you test; some include quotes or a trailing space.

Sign in with BasicAuth

BasicAuth (in Example1-AuthAndTables) is the minimal sign-in script: it creates the client and signs in the current user with their SPECS Account. After a successful sign-in it has the user's ID and session, which every other Snap Cloud service needs. It isn't placed in the prefab; add it to a scene object to try it.

BasicAuth reads the project from its snapCloudRequirements input. Its own Supabase Project input is deprecated and used only when snapCloudRequirements isn't set.

The code is the same as the SnapCloudStarter script in Create the client and sign in. If sign-in returns AuthRetryableFetchError because the token isn't ready at startup, see Troubleshooting; the Media examples in this package include retry logic you can reuse.

Rather than giving every script its own project input, the examples share one SnapCloudRequirements component: see Share one project across scripts.

Read and write tables with TableConnector

The Tables example (TableConnector, in Example1-AuthAndTables) reads and writes three tables: a primary table, an event log (user_interactions), and per-user settings (user_preferences).

Create the tables in the Snap Cloud dashboard:

TableColumns
test_tablemessage, sender, timestamp, lens_session_id
user_interactionsaction, data, timestamp, session_id
user_preferencesuser_id (unique), preferences, updated_at

Mock rows for user_interactions and user_preferences ship in ExternalServicesExamples/example-1to4-mockup-data/testData-ADD TO TABLES/; import them into the tables of the same name. No test_table file ships, so create that table yourself. Turn on RLS for each table and add policies that allow the operations the example runs.

The panel's table button calls retrieveLatestData(), which reads a few rows from each table. It's attached to the button's onTriggerUp event (a UI Kit RectangleButton), so users pull fresh data on demand instead of the Lens polling. A separate public method, testConnection(), also writes sample rows; nothing in the prefab calls it, so use a test backend if you call it.

To learn the code:

Sync a cursor with RealtimeCursor

The Realtime Cursor example (RealtimeCursor, in Example2-RealTime) keeps a Realtime channel open in one of two modes, toggled by the panel's realtime button:

  • Broadcast sends the arrow cursor's position to every subscriber, starting as soon as the channel connects.
  • Follow moves the cursor to positions received from another client, such as a web browser.

To set it up:

  1. On the RealtimeCursor component, replace the Channel Name placeholder with your own name, for example spectacles-demo-channel. The script joins the channel cursor-<channel name>.
  2. Optional: to drive or watch the cursor from a browser, copy ExternalServicesExamples/realtime-example-web-cursor-controller/ out of the project and serve it (npm start serves it with http-server).
  3. On the web page, enter the same channel name, without the cursor- prefix.

The Lens sends its positions with spectacles_ user IDs. In follow mode it accepts positions only from pc_ user IDs, the IDs the web page uses. No table is needed; an optional cursor_debug table receives a debug row every 50 broadcasts.

The code, the coordinate mapping, and broadcast throttling: Stream a cursor position between a Lens and a web page.

Load models, images, and audio with StorageLoader

The Storage example (StorageLoader, in Example3-Storage) downloads three types of files from a public Storage bucket when you press the panel's storage button: a 3D model, instantiated as a scene object; an image, applied to an Image component; and an audio file, played through an AudioComponent. All three downloads run at the same time.

  1. In the Snap Cloud dashboard, create a public bucket, for example specs-bucket. The example loads files from their public URLs, so downloads from a private bucket fail.
  2. Upload the sample files from ExternalServicesExamples/example-1to4-mockup-data/testAssets-ADD TO STORAGE BUCKET/ (rabbit.glb, glasses.jpg, and chill.mp3) in folders by type: models/rabbit.glb, images/glasses.jpg, audio/chill.mp3.
  3. Under Storage > Policies, add a policy that grants authenticated users SELECT access.
  4. On the StorageLoader component, replace the bucket and file path placeholders with your bucket name and the paths you uploaded. The prefab suggests images/spectacles.jpg, which doesn't match the sample image's name.
  5. Assign the image, audio, and material references in the Inspector.

The code, model scale, and downloading in parallel: Storage.

Call an edge function with EdgeFunctionImgProcessing

The Edge Functions example (EdgeFunctionImgProcessing, in Example4-EdgeFunctions) sends the URL of an image to an edge function, which downloads the image, stores a copy in Storage, and returns the copy's URL. The Lens then downloads and shows that image. The function does no image processing: it's a starting point for your own.

  1. In the Snap Cloud dashboard, deploy the function in ExternalServicesExamples/example-1to4-mockup-data/testEdgeFunction-ADD TO EDGE FUNCTION CODE/index.txt: paste it as the function's index.ts. It stores the copy in specs-bucket/images/ with the service role key from the function's environment, and returns success, processedUrl, operations, the file sizes, and storagePath.
  2. Turn off Verify JWT with legacy secret for the function: see Edge Functions.
  3. On the EdgeFunctionImgProcessing component, set Function Name to the name you deployed, replacing the placeholder.
  4. Set Image Url to the URL of a public image.

Deploying functions, the service role key, and why the Lens sends a URL rather than the image bytes: Process a Storage image in an edge function.