Back to Tutorials
40 min
Advanced
Working with Data and APIs
Connect your projects to external data sources, integrate with APIs, use Supabase for databases, and build dynamic, data-driven applications.
Introduction
Data is what makes applications dynamic and useful. In this tutorial, you'll learn how to connect Structor projects to databases, integrate with APIs, and build data-driven applications using Supabase and HTTP requests.
Step 1: Setting Up Supabase
1.1 Create a Supabase Project
- Go to supabase.com and sign up/login
- Create a new project
- Wait for the project to initialize
1.2 Connect Supabase to Structor
- In Structor, open Settings (gear icon)
- Go to the Supabase tab
- Enter your Supabase URL and API Key (from your Supabase project settings)
- Click Test Connection to verify
- Click Save
Step 2: Create a Database Table
2.1 Using Backend Studio
- Click the Database icon in the top bar (opens Backend Studio)
- In the Database section, click + New Table
- Name your table (e.g.,
products) - Add columns:
id- Type: Text, Primary Keyname- Type: Textprice- Type: Numberdescription- Type: Text
- Click Create Table
2.2 Add Sample Data
- In the Backend Studio, select your table
- Click Add Row to insert sample data
- Fill in the fields and save
- Add a few more rows
Step 3: Fetch Data from Supabase
3.1 Create App State Variable
- Open Settings > App State
- Add a variable named
products - Set type to:
Array - Leave default value empty:
[]
3.2 Create Server Action
- Open Settings > Server Actions
- Click + Add Server Action
- Set Action ID to:
fetchProducts - Add code:
async function fetchProducts(args) { const { supabase } = context; const { data, error } = await supabase .from('products') .select('*'); if (error) throw error; return data; } - Click Save
3.3 Call Server Action from Page
- Add a Button labeled "Load Products"
- Add On Click action
- In the flow editor, add Run Server Action:
- Action ID:
fetchProducts - Output Variable:
products
- Action ID:
- Add Show Toast with message: "Products loaded!"
Step 4: Display Data in a List
4.1 Create List Container
- Add a Div container (this will be your list)
- Style it with Display: Flex, Flex Direction: Column, Gap: 16px
4.2 Create Item Template
- Inside the container, add a Div (this is your item template)
- Add Text elements inside it:
- One for product name: Set content to
{{item.name}} - One for price: Set content to
${{item.price}} - One for description: Set content to
{{item.description}}
- One for product name: Set content to
- Style the item template as a card
4.3 Configure Dynamic List
- Select the item template div
- Open the Data tab in the right panel
- Enable Repeat for Each
- Set Data to repeat:
products - Set Current item called:
item - The list will now repeat for each product!
Step 5: Make HTTP Requests to External APIs
- Add a button labeled "Fetch Data"
- Add On Click action
- In the flow editor, add HTTP Request:
- Method: GET
- URL:
https://api.example.com/data - Output Variable:
apiResponse
- Add Conditional to check for errors
- Add Show Toast for success/error messages
Step 6: Handle Errors
- After your HTTP Request, add a Conditional node
- Check if the response has an error (e.g.,
{{apiResponse.error}} != null) - In the "true" branch, show an error toast
- In the "false" branch, show success and update state
Step 7: Create Form to Add Data
7.1 Create Form
- Add Input elements for name, price, description
- Set input bindings:
newName,newPrice,newDescription - Add a submit button
7.2 Create Server Action to Insert
- In Settings > Server Actions, add action:
addProduct - Add code:
async function addProduct(args) { const { supabase } = context; const { data, error } = await supabase .from('products') .insert([{ name: args.name, price: args.price, description: args.description }]); if (error) throw error; return data; }
7.3 Connect Form to Server Action
- On submit button, add On Click action
- Add Run Server Action:
- Action ID:
addProduct - Arguments:
{"name": "{{newName}}", "price": newPrice, "description": "{{newDescription}}"}
- Action ID:
- After the server action, call
fetchProductsagain to refresh the list
Congratulations!
You've learned how to:
- Set up Supabase integration
- Create database tables
- Create server actions
- Fetch and display data
- Make HTTP requests
- Handle errors
- Create forms to add data
Next Steps
- Learn about version control for managing changes
- Explore advanced data features in the documentation
- Set up Row Level Security (RLS) in Supabase for production apps