Skip to main content
To reuse existing spaces with embedded Flatfile, you need a server-side component that retrieves space access tokens using your secret key. This guide shows you how to set up the server-side pattern correctly.

Why Server-Side Setup is Required

When reusing an existing space, you cannot use your publishable key directly. Instead, you must:
  1. Server-side: Use your secret key to retrieve the space and its access token
  2. Client-side: Use the access token (not publishable key) to launch the space
This pattern ensures security by keeping your secret key on the server while providing the necessary access token to the client.

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

  1. Start your server: node server.js
  2. Test the endpoint: curl http://localhost:3001/api/spaces/us_sp_abc123def456
  3. Verify the response includes id and accessToken

Production Deployment

  1. Set environment variables on your server
  2. Deploy your server application
  3. Update your client to use the production server URL
  4. 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
For client-side SDK implementation, see the framework-specific guides: