Setting Up Your First Listener
Listeners are the core of Flatfile’s event-driven architecture. They respond to events throughout the data import lifecycle, from space configuration to data validation and job processing. This guide will help you understand how to create and organize listeners effectively.
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!
About Listeners
Listeners handle different types of events in Flatfile:
- Space Configuration: Setting up workbooks, sheets, and themes
- Data Validation: Custom field and record-level validation
- Job Processing: Handling long-running operations
- File Operations: Processing uploaded files
- User Interactions: Responding to custom actions
for more details, see Listeners
Scoping Listeners
Each listener is tied to a specific environment using the FLATFILE_ENVIRONMENT_ID
environment variable. This ensures the listener only processes events from its assigned environment.
For more granular control, you can use listener.namespace()
to organize and filter events. Namespaces let you create listeners that only respond to events from specific spaces, workbooks, or other scoped contexts within your environment.
For more details and namespace patterns, see Scoping with Namespaces.
Install Dependencies
Install the required packages for building Flatfile listeners:
Create Your Listener File
Create a new file called listener.js
(or listener.ts
for TypeScript):
Testing and Deployment
For complete testing and deployment documentation, see the CLI Reference.
Authentication Setup
For complete authentication setup examples, see the Authentication Examples guide.
Local Development
To test your listener locally, you can use the flatfile develop
command. This will start a local server that will listen for events and respond to them, and will also watch for changes to your listener code and automatically reload the server.
Deploy to Flatfile Cloud
Deploying your listener will create a new Agent in your Flatfile environment. Under the hood, this will compile all of your listener code and its dependencies into a single file, which it sends to the Flatfile API’s POST /v1/agents
endpoint.
That’s it! Your listener will:
- Create a workbook when a space is opened
- Process data when users click Submit
- Handle the complete data import workflow
What Just Happened?
Your minimal listener handles two key events:
space:configure
- Sets up the data structure (workbook + sheets)job:ready
- Processes data when users submit
Common Patterns
Add validation:
- This example sets up a bare metal event listener on the
commit:created
event - Depending on your use case, a better pattern may be to use the Record Hooks plugin, which provides a more object-oriented approach to updating records.
Add a custom action: