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 ({ context: { jobId, workbookId }, payload }) => {
    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,
      });

      console.log(JSON.stringify(sheets, null, 2));

      const webhookReceiver =
        process.env.WEBHOOK_SITE_URL ||
        "https://webhook.site/c83648d4-bf0c-4bb1-acb7-9c170dad4388"; //update this

      const response = await axios.post(
        webhookReceiver,
        {
          ...payload,
          method: "axios",
          workbook: {
            ...workbook,
            sheets,
          },
        },
        {
          headers: {
            "Content-Type": "application/json",
          },
        }
      );

      if (response.status === 200) {
        const rejections = response.data.rejections;
        if (rejections) {
          return await responseRejectionHandler(rejections);
        }
        return 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");
      }
    } catch (error) {
      console.log(`webhook.site[error]: ${JSON.stringify(error, null, 2)}`);

      await api.jobs.fail(jobId, {
        outcome: {
          message:
            "This job failed probably because it couldn't find the webhook.site URL.",
        },
      });
    }
  }
);
// 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.