Reuse an existing Space
Reuse a Space when Flatfile is opened.
For applications meant to use the same space consistently, open an existing space each time Flatfile is opened. This suits situations where consistently editing a dataset is preferred.
Before you begin
If you have already tried our Embed a New Space guide you will notice this guide departs heavily, so you will want to create this in a new directory, as translation would be more difficult than creating from scratch.
Get your keys
To complete this tutorial, you'll need to retrieve your Secret
key from your development
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.
Make a new Directory.
Go into that directory.
Follow prompts from the init
command.
Install Packages
Create your file structure
The file structure of this application is fairly complex, as it requires a dedicated server and API. You can create an api endpoint externally if desired to keep your own application less complex, however this application is an all-in-one application server and api solution.
In order to create this solution, create a file structure like the following:
In this file structure, you will primarily work out of /src
especially in /src/components
. Many of thsese files are configuration files which are mostly set once and not touched again. So while there may be a lot, they won’t be actively managed.
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 can be found on your Dashboard where it lists your Spaces. You may also want to include your PUBLISHABLE_KEY
and ENVIRONMENT_ID
if you want to have your app create new spaces as well.
Note in the below example some of the variables are prefixed with VITE_
. This is because Vite requires the prefix to access them at runtime. The FLATFILE_API_KEY
should never be accessible from the browser for security reasons, and should not have this prefix.
Build your importer
Before starting to build the application, it is important to note that there are a few moving parts, and it won’t be able to start up between each update that is being made. This guide will endeavor to break out files into groups, so that after everything is updated as directed, the app should start up without errors.
1. Set up Configuration Files
This app has several configuration files that must be set up before you can get started with development. Set them up as shown below.
You will also want to add some scripts to your package.json
to start the app. Add the following scripts:
Please note that these scripts include files that haven’t been created yet. These will be created in the following steps.
2. Create the EJS HTML and Server
This app has a home page to route the user to create a new space or embed an existing space. While the former choice is optional and not the focus of this guide (although instructions on how to include this function will be included), the files edited here set up functionality for embedding existing spaces, and as such is still necessary.
This application utilizes ejs, which helps build dynamic html pages. This allows us to dynamically create both development and production environment outputs from one file. Set this up at /views/index.html.ejs
This file wil not work before the server is functioning, but is required for the rest of the app to work.
Next lets create the server that will act as the backend of the application. This will be necessary to serve the pages as well as get the existing space, as due to security reasons the Secret Key cannot be exposed to the browser at any time.
In addition to the server/index.ts
, you will need to create two routers - One for the homepage and one for the assets. This is because the frontend runs on a different port than you’ll be accessing the app from, so the app will need a bit of help resolving pathing.
Create server/homepageRouter.ts
and server/assetsRouter.ts
as follows, then update server/index.ts
.
3. Build the App Component
Before building the components, you’ll need to make a couple TypeScript Declaration files. This is so your IDE won’t throw unnecessary errors at you during development.
You’ll need a src/vite-env.d.ts
file and a src/shims-vue.d.ts
file in order to sort your the declarations. Create them as follows:
Next you’ll need a src/main.ts
file to mount src/App.vue
to your HTML. Create it and your App Component as shown below.
4. Build your Home & Existing Space Component
This app is built around a Home component that is loaded first and lets you navigate the app. That component is fairly basic and should look like this:
Now you’ll need to build the component that will get your space and return it to the browser.
The component should end up looking something like this:
You’ll need to update your App.vue
to include the existing space component. It should end up looking like the example below:
It may be wise to set up your styles in src/styles/styles.css
. These should end up looking like the example below:
Remember to add import './styles/styles.css'
to your src/App.vue
with the other imports to apply the styles.
5. Optional - Build your New Space Component
If you want to have a page to create new spaces, you can create a component to do that fairly easily. The vue code should look fairly similar to Existing Space, just with some different configuration settings. See below for the necessary changes.
6. Start your client
Now you should be able to start your app. To load it in dev mode and ensure everything works proprly, run:
If you have any errors, now is your time to fix them, otherwise - you’re ready to deploy!
7. Customize
You can stop here or you can view our full reference to see all the ways you can customize your importer.
Example Project
Find this Vue.js example project in the Flatfile GitHub repository.