Making API Calls
Learn how to make API calls with and without authentication in Indie Kit using SWR and fetch
Making API Calls 🔌
Let's explore how to make API calls in Indie Kit. We'll cover both authenticated and public endpoints using SWR and fetch. 🚀
Public API Calls 🌐
These endpoints don't require authentication. Perfect for public data like blog posts or product listings.
Backend Implementation
Frontend Implementation
Protected API Calls 🔒
These endpoints require authentication. Perfect for user-specific data or admin actions.
Backend Implementation
Frontend Implementation
Using SWR Effectively 🎯
SWR (Stale-While-Revalidate) is already configured in Indie Kit. Here's how to use it:
-
Basic Fetching
-
With Mutation
-
Conditional Fetching
💡 Pro Tip Read more about SWR features in the official documentation.
Best Practices 💫
-
Error Handling 🚨
-
Loading States ⌛
-
Optimistic Updates ✨
-
Type Safety 🛡️
Security Tips 🔐
- Always use
withAuthRequired
for protected routes - Validate request bodies server-side
- Use proper HTTP methods (GET for fetching, POST for creating, etc.)
- Handle errors gracefully on both client and server
Now you're ready to build powerful features with API calls in Indie Kit! 🚀