Skip to main content

Pre-Hook Option

This tutorial shows how to use the --pre-hook option in the FormKiQ CLI to add document tags and actions while syncing documents to FormKiQ.

What You Will Build​

You will build a small Node.js web service that receives FormKiQ CLI pre-hook requests and returns document-specific tags. You will then sync two sample files and have the CLI apply different tags based on the file path.

Before You Begin​

  • Access to a FormKiQ Essentials, Advanced, or Enterprise installation.
  • The FormKiQ CLI installed and configured.
  • Node.js installed.
  • A terminal with access to run fk and node.

Workflow Overview​

  1. Create a Node.js pre-hook web service.
  2. Test the pre-hook endpoint directly.
  3. Create two sample files.
  4. Run the FormKiQ CLI with --pre-hook.
  5. Verify the files and tags in FormKiQ.

Step 1: Create the Pre-Hook Web Service​

To create a Node.js web service that processes the given request and returns the specified payloads based on the filename, follow these steps:

Initialize a Node.js Project​

First, set up your Node.js project. In your terminal, run:

mkdir webservice
cd webservice
npm init -y

Install Required Packages​

You will need Express, a popular web framework for Node.js. Install it by running:

npm install express

Create the Web Service​

Create a new file named index.js and add the following code:

const express = require('express');
const app = express();
const port = 3000;

// Middleware to parse JSON bodies
app.use(express.json());

// Define the payloads
const payloads = {
"./examples/example1.txt": {
"tags": [
{
"key": "category",
"value": "document"
},
{
"key": "user",
"values": ["100", "101"]
}
],
"actions": [
{
"type": "FULLTEXT"
}
]
},
"./examples/example2.txt": {
"tags": [
{
"key": "category",
"value": "invoice"
},
{
"key": "user",
"values": ["200", "201"]
}
],
"actions": [
{
"type": "OCR"
}
]
}
};

// Endpoint to handle requests
app.post('/get-payload', (req, res) => {
const filename = req.body.path;
const payload = payloads[filename];

if (payload) {
res.json(payload);
} else {
res.status(404).json({ error: "File not found" });
}
});

// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});

Step 2: Run the Web Service​

In your terminal, run the following command to start the server:

node index.js

You should see the message Server is running on http://localhost:3000.

Step 3: Test the Web Service​

You can use tools like curl or Postman to test your web service.

Using curl​

For example1.txt:

curl -X POST http://localhost:3000/get-payload -H "Content-Type: application/json" -d '{"path":"./examples/example1.txt"}'

Expected response:

{
"tags": [
{
"key": "category",
"value": "document"
},
{
"key": "user",
"values": ["100", "101"]
}
],
"actions": [
{
"type": "FULLTEXT"
}
]
}

For example2.txt:

curl -X POST http://localhost:3000/get-payload -H "Content-Type: application/json" -d '{"path":"./examples/example2.txt"}'

Expected response:

{
"tags": [
{
"key": "category",
"value": "invoice"
},
{
"key": "user",
"values": ["200", "201"]
}
],
"actions": [
{
"type": "OCR"
}
]
}

If you request a non-existent file, like example3.txt:

curl -X POST http://localhost:3000/get-payload -H "Content-Type: application/json" -d '{"path":"example3.txt"}'

Expected response:

{
"error": "File not found"
}

Step 4: Create Sample Files​

Now create the sample files that the FormKiQ CLI will upload.

Create a directory named examples, then create example1.txt and example2.txt.

Create a new directory:

mkdir examples

Create two files in the examples directory.

examples/example1.txt

This is example1.txt

examples/example2.txt

This is example2.txt

Step 5: Configure FormKiQ CLI​

If the CLI is not already configured, configure it using the FormKiQ CloudFormation outputs:

fk --configure --access-key ACCESS_KEY \
--secret-key ACCESS_SECRET \
--region AWS_REGION \
--iam-api-url IAM_API_URL \
--documents-dynamodb-tablename DOCUMENTS_TABLE_NAME

Step 6: Run FormKiQ CLI​

We will now run the FormKiQ CLI to sync 2 sample files from the examples directory to FormKiQ.

fk --sync -d ./examples --pre-hook http://localhost:3000/get-payload -v

Verify the Result​

Open FormKiQ Web and confirm that example1.txt and example2.txt were uploaded. Check each document's tags and queued actions to confirm the pre-hook response was applied.

Clean Up​

Stop the Node.js service with Ctrl+C. Delete the sample documents from FormKiQ if you do not want to keep them.

Troubleshooting​

ProblemLikely causeWhat to check
The FormKiQ CLI receives a 404 from the pre-hookThe file path in the request does not match the payloads map.Log req.body.path and update the sample keys.
Documents upload without tagsThe pre-hook URL is wrong or the service is not running.Confirm node index.js is running and --pre-hook points to the correct URL.
Actions are not createdThe returned action type or payload is invalid.Use supported action types such as FULLTEXT, OCR, or WEBHOOK.

Next Steps​

Privacy settings

Analytics is optional. Your analytics choice is shared with formkiq.com and remembered for up to six months. Change it here at any time.

Essential preferences and search

We remember your privacy choice and display preferences. Search runs in your browser using Pagefind files hosted with the docs. It works whether you accept or reject analytics.

Measure page views and installation-link clicks. This does not enable advertising on the docs. Existing marketing choices for the main website are kept unchanged.

Read the docs privacy notice or manage other main-website privacy choices.