The DevXP engineering team hosts office hours every Thursday at 11 a.m. Pacific Time where we answer your questions live and help you get up and running with Flatfile. Join us!

After your customers have imported their data, the process of transmitting that data to an external destination or network is referred to as “egress out.”

Egress via Workbook-level Action

This code listens for a job event of type workbook:submitAction and when triggered, it retrieves the Sheets and records associated with the workbook.

It then prepares the data and sends a POST request to a specified webhook URL using Axios. The payload includes information about the Sheets, records, and other relevant data.

If the request is successful, it marks the job as complete. If there is an error, it marks the job as failed.

Testing with webhook.site

  1. To create the receiver, navigate to webhook.site
  2. Copy the value of the path after webhook.site for Your unique URL.
  3. Next, add the following to your default export function.
//your workbook should have an action that looks like this
actions: [
  {
    operation: 'submitActionFg',
    mode: 'foreground',
    label: 'Submit data elsewhere',
    type: 'string',
    description: 'Submit this data to a webhook.',
    primary: true,
  },
  {...}
],
settings: {
  trackChanges: true,
}
red.on(
  "job:ready",
  { job: "workbook:submitAction" },
  async (event: FlatfileEvent) => {
    const { jobId, workbookId } = event.context;
    const { data: workbook } = await api.workbooks.get(workbookId);
    const { data: workbookSheets } = await api.sheets.list({ workbookId });

    const sheets = [];
    for (const [_, element] of workbookSheets.entries()) {
      const { data: records } = await api.records.get(element.id);
      sheets.push({
        ...element,
        ...records,
      });
    }

    try {
      await api.jobs.ack(jobId, {
        info: "Starting job to submit action to webhook.site",
        progress: 10,
      });

      // TODO: place your webhook url here
      const webhookReceiver = "https://webhook.site/...";

      const response = await fetch(webhookReceiver, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          workbook: {
            ...workbook,
            sheets,
          },
        }),
      });

      if (response.status === 200) {
        const responseData = await response.json();
        const rejections = responseData.rejections;

        if (rejections) {
          const outcome = await responseRejectionHandler(rejections);
          await api.jobs.complete(jobId, outcome);
        }
        await api.jobs.complete(jobId, {
          outcome: {
            message: `Data was successfully submitted to webhook.site. Go check it out at ${webhookReceiver}.`,
          },
        });
      } else {
        throw new Error("Failed to submit data to webhook.site");
      }
      return;
    } catch (error) {
      console.error(error);
      await api.jobs.fail(jobId, {
        outcome: {
          message:
            "This job failed probably because it couldn't find the webhook.site URL.",
        },
      });
      return;
    }
  }
);
// See full code example (https://github.com/FlatFilers/flatfile-docs-kitchen-sink/blob/main/javascript/shared/workbook_submit.js)

Example Project

Find the egress example in the Flatfile GitHub repository.