Before you begin
To reuse an existing Space, we’ll update our files to pass in a Space Id instead of apublishableKey. We’ll then make a server-side request using our secretKey to get the Space and its access token.
Get your keys
To complete this tutorial, you'll need to retrieve your Secret key from yourdevelopment environment.
Note: Unlike the Publishable Key, the Secret Key shouldn’t be passed through
the browser as it will have full access. This is why we are showing this
example in a server-side call.
Check for proper guestAuthentication
You can skip this step if you haven't created a custom Environment in your
Account.
guestAuthentication configured with shared_link as an
option. This is inherited from the Environment you're working in, and more info can be found in your Developer Settings page in the platform.
shared_link comes standard on all out-of-the-box Environments (develop / production) so you shouldn't have to do anything unless you have created a custom Environment.
Prepare your project
Install packages
Make a new directory.init command.
Create your file structure
Setup your app to look something like this:public and src.
The public directory contains the index.html file, which is the entry point of the application’s front-end, and the “style.css” file for styling the iframe.
The src directory contains the main components and logic of the application, including the client.js file, which initializes Flatfile and passes in available options. And the server.mjs file, which sets up a Node.js server using Express that listens for incoming requests and communicates with the Flatfile API to retrieve data about a specified Space.
Update your .env
Update your .env.FLATFILE_API_KEY is your Secret Key and SPACE_ID is the Space you want to open in the importer. This is can be found on your Dashboard where it lists your Spaces. You shouldn’t need to update the BASE_URL.
Build your importer
1. Add a Flatfile button
Add a button to your application to open Flatfile in a modal. Pass in yourpublishableKey and a new Space will be created on each page load. Also, add the content here to your styles.css.
2. Create a local server
This code sets up a Node.js server using Express that listens for incoming requests, enables CORS (Cross-Origin Resource Sharing), and communicates with the Flatfile API to retrieve data about a specified Space based on the provided environment variables.src/server.mjs
3. Start your server
Now, start your server by heading to terminal and running the following command. To see that it’s running, visit: https://localhost:8080 (or the port it is running on) and you should see a page that says “Hello, World”.4. Initialize Flatfile
In yourclient.js, at minimum, you’ll need to get and pass the space info. This code opens an existing Space in a modal by making a request to your server endpoint and initializing the Flatfile data import with specified options based on the server’s response.
src/client.js