If you aren’t interested in a code-forward approach, we recommend starting with AutoBuild, which uses AI to analyze your template or documentation and then automatically creates and deploys a Blueprint (for schema definition) and a Listener (for validations and transformations) to your Flatfile App.Once you’ve started with AutoBuild, you can always download your Listener code and continue building with code from there!
What We’re Building
In this tutorial, we’ll build a foundational Listener that handles Space configuration - the essential first step for any Flatfile implementation. Our Listener will:- Respond to Space creation: When a user creates a new Space, our Listener will automatically configure it
- Define the Blueprint: Set up a single Workbook with a single Sheet with Field definitions for names and emails that establishes the data schema for the Space
- Handle the complete Job lifecycle: Acknowledge, update progress, and complete the configuration Job with proper error handling
- Provide user feedback: Give real-time updates during the configuration process
Prerequisites
Before we start coding, you’ll need a Flatfile account and a fresh project directory:- Create a new project directory: Start in a fresh directory for this tutorial (e.g.,
mkdir my-flatfile-listener && cd my-flatfile-listener
) - Sign up for Flatfile: Visit platform.flatfile.com and create your free account
- Get your credentials: You’ll need your Secret Key and Environment ID from the Keys & Secrets section later in this tutorial
New to Flatfile? If you’d like to understand the broader data structure and concepts before diving into code, we recommend reading through the Core Concepts section first. This covers the foundational elements like Environments, Apps, and Spaces, as well as our data structure like Workbooks and Sheets, and how they all work together.Each Listener is deployed to a specific Environment, allowing you to set up separate Environments for development, staging, and production to safely test code changes before deploying to production.
Install Dependencies
Choose your preferred language and follow the setup steps:Authentication Setup
For this step, you’ll need to get your Secret Key and environment ID from your Flatfile Dashboard. Then create a new file called.env
and add the following (populated with your own values):
Create Your Listener File
Create a new file calledindex.js
for Javascript or index.ts
for TypeScript:
Complete Example: The full working code for this tutorial step is available in our Getting Started repository: JavaScript | TypeScript
Project Structure
After creating your Listener file, your project directory should look like this:Authentication Setup
You’ll need to get your Secret Key and Environment ID from your Flatfile Dashboard to find both values, then add them to a.env
file:
Testing Your Listener
Local Development
To test your Listener locally, you can use theflatfile develop
command. This will start a local server that implements your custom Listener code, and will also watch for changes to your code and automatically reload the server.
Step-by-Step Testing
After running your listener locally:Testing Steps
- Create a new space in your Flatfile environment
- Observe as the new space is configured with a Workbook and Sheet
What Just Happened?
Your Listener is now ready to respond to Space configuration Events! Here’s how the space configuration works step by step:1. Exporting your Listener function
This is the base structure of your Listener. At its core, it’s just a function that takes alistener
object as an argument, and then uses that listener to respond to Events.
2. Listen for Space Configuration
When a new Space is created, Flatfile automatically triggers aspace:configure
job that your Listener can handle. This code listens for that job using the job:ready
Event, filtered by the job name space:configure
.
3. Acknowledge the Job
The first step is always to acknowledge that you’ve received the job and provide initial feedback to users. From this point on, we’re responsible for the rest of the job lifecycle, and we’ll be doing it all in this Listener. For more information on Jobs, see the Jobs concept.4. Define the Blueprint
Next, we create the workbook with sheets and field definitions. This is your Blueprint definition—establishing the data schema that will govern all data within this Space.5. Update Progress
Keep users informed about what’s happening during the configuration process.6. Complete the Job
Finally, mark the job as complete with a success message, or fail it if something went wrong.Next Steps
Ready to enhance data quality? Continue to Adding Validation to learn how to validate Fields and provide real-time feedback to users. For more detailed information:- Understand Job lifecycle patterns in Jobs and Spaces
- Learn more about Events
- Organize your Listeners with Namespaces