Why Server-Side Setup is Required
When reusing an existing space, you cannot use your publishable key directly. Instead, you must:- Server-side: Use your secret key to retrieve the space and its access token
- Client-side: Use the access token (not publishable key) to launch the space
Server-Side Implementation
Environment Setup
Create a.env
file with your credentials:
Basic Server Example
Here’s a Node.js/Express server that retrieves space access tokens:Enhanced Server with Multiple Spaces
For applications that need to handle multiple spaces:Client-Side Implementation
Fetching Space Data
Your client application should fetch the space data from your server:Using the Space Data
Once you have the space data, use it to initialize Flatfile:Complete Workflow Example
Here’s how the complete workflow works:1. Server Setup
2. Client Request
3. Client Usage
Security Considerations
Environment Variables
Never expose your secret key in client-side code:CORS Configuration
Configure CORS to only allow your frontend domain:Access Token Handling
- Access tokens are temporary and space-specific
- They should be fetched fresh for each session
- Store them securely in your client application
Error Handling
Server-Side Errors
Client-Side Errors
Testing
Local Development
- Start your server:
node server.js
- Test the endpoint:
curl http://localhost:3001/api/spaces/us_sp_abc123def456
- Verify the response includes
id
andaccessToken
Production Deployment
- Set environment variables on your server
- Deploy your server application
- Update your client to use the production server URL
- Test the complete workflow
Next Steps
Once your server is set up:- Update your client applications to use the server endpoint
- Test the space reuse functionality
- Monitor server logs for any issues
- Consider adding authentication for your server endpoints