Tutorial

How to Extract Specific Information from Documents Using Question Answering with JavaScript

This guide shows how to implement document question answering in JavaScript using Eden AI’s asynchronous API. It explains the two-stage process launching a job and retrieving results using Axios for HTTP requests.

How to Extract Specific Information from Documents Using Question Answering with JavaScript
TABLE OF CONTENTS

In this tutorial, you will learn how to use the Document Question Answering API in 5 minutes using JavaScript. Eden AI provides an easy and developer-friendly API that allows you to extract specific information from your documents.

What is Document Question Answering API?

Document Question Answering allows the extraction of specific information from text-based documents using a query.

Simply provide the text you want to search, and the system will use optical character recognition (OCR) technology to scan the document and extract the information you need by automatically finding the specific data within large amounts of text.

This makes it ideal for tasks such as data entry and document analysis.

Getting Started with Document Question Answering

Set Up Your Eden AI Account

1. Sign Up: If you don't have an Eden AI account, create a free here. Once you do that you can obtain your API key, which you can use to access Document Question Answering.

2. Access OCR Tools: Once logged in, go to the document parsing section of the platform.

3. Choose the Document Question Answering Feature: Select the Document Question Answering tool. You can also explore advanced parsing options based on your specific requirements.

How to Implement Document Question Answering API in JavaScript?

If you haven’t already, install axios to handle HTTP requests in Node.js:


npm install axios

Eden AI uses an asynchronous job system:

  • POST: Launches the job without waiting for processing to complete.
  • GET: Separately retrieves the result once the job is finished. This method ensures reliability and scalability when processing large or complex documents.

Launch the Document Parsing Job (POST Request)


const axios = require("axios").default;

const options = {
  method: "POST",
  url: "https://api.edenai.run/v2/ocr/custom_document_parsing_async",
  headers: {
    Authorization: "Bearer YOUR_API_TOKEN", // Replace with your Eden AI API key
  },
  data: {
    providers: "amazon",
    queries: "[{'query': 'What is the total invoice', 'pages': '1-*'},{'query': 'what is the invoice date','pages': '1'}]",
    file_url: "https://your-doc-url.com/invoice.pdf" // Replace with your file URL
  }
};

axios
  .request(options)
  .then((response) => {
    console.log("Job launched successfully:", response.data);
  })
  .catch((error) => {
    console.error("Launch error:", error);
  });

Explanation: What This Code Does

  • Method: Sends a POST request to Eden AI’s OCR async endpoint.
  • Authorization: Uses your API token to authenticate.
  • Data:

    • providers: Selects the AI provider (e.g., "amazon").
    • queries: A list of questions to extract answers from the document.
    • file_url: Public link to the document (image or PDF).

Response: Returns a public_id used to track the job’s status.

Get the Job Results (GET Request)

Once the job is submitted, Eden AI processes it asynchronously. You use the public_id from the POST response to fetch results.


const axios = require('axios');

const publicId = 'your_public_id_here'; // Use the public_id from the previous response
const apiKey = 'YOUR_EDENAI_API_KEY';

axios.get(`https://api.edenai.run/v2/ocr/custom_document_parsing_async/${publicId}/`, {
  headers: {
    Authorization: `Bearer ${apiKey}`
  }
})
.then(response => {
  console.log('Job result:', response.data);
})
.catch(error => {
  console.error('Error fetching job result:', error.response?.data || error.message);
});

Example Output


{
  "amazon": {
    "results": [
      {
        "query": "What is the total invoice",
        "answer": "$1,250.00",
        "page": 1
      },
      {
        "query": "what is the invoice date",
        "answer": "2023-12-05",
        "page": 1
      }
    ],
    "status": "success",
    "cost": 0.002
  }
}

Explanation of Output:

  • query: The original question.
  • answer: Extracted answer from the document.
  • page: Page number where the answer was found.
  • status: Status of the job (e.g., success).
  • cost: API usage cost (if applicable).

Benefits of using Document QA API with Eden AI

Using Document QA with Eden AI API is quick and easy.

Multiple AIs in one API - Eden AI
Multiple AIs in one API - Eden AI

Save time and cost

We offer a unified API for all providers: simple and standard to use, with a quick switch between providers and an access to the specific features of each provider.

Easy to integrate

The JSON output format is the same for all suppliers thanks to Eden AI's standardisation work. The response elements are also standardised thanks to Eden AI's powerful matching algorithms.

Customization

With Eden AI you have the possibility to integrate a third party platform: we can quickly develop connectors. To go further and customize your Document QA request with specific parameters, check out our documentation.

Conclusion

  • Document Q&A allows you to extract precise answers from documents automatically.
  • Eden AI’s two-stage async API makes this easy with a launch-and-fetch workflow.
  • JavaScript with Axios provides a smooth integration path.

If you're building applications that handle document understanding like invoice parsing, contract analysis, or form processing, Eden AI offers a powerful and developer-friendly solution with minimal code and maximum flexibility.

Start Your AI Journey Today

  • Access 100+ AI APIs in a single platform.
  • Compare and deploy AI models effortlessly.
  • Pay-as-you-go with no upfront fees.
Start building FREE

Related Posts

Try Eden AI for free.

You can directly start building now. If you have any questions, feel free to chat with us!

Get startedContact sales
X

Start Your AI Journey Today

Sign up now with free credits to explore 100+ AI APIs.
Get my FREE credits now