How to Convert Data Files in Node.js with the Konbert API

Konbert is a file conversion service that offers an API to convert files between various formats like CSV, JSON, Avro, or Arrow. In this article, we will learn how to use the Konbert API's /convert endpoint to convert files in Node.

How Does the Konbert API Work?

The Konbert API allows users to convert files between different formats by making HTTP requests to the /convert endpoint. Users need to authenticate with the API using their API key, which is available after signing up for an account.

Konbert supports a wide range of input and output formats, including CSV, JSON, Avro, Arrow, and many more. Users can provide the input file either by uploading the file or by providing a URL.

Converting a File using Node with Fetch

To convert a file using the Konbert API, we need to make an HTTP POST request to the /convert endpoint with the appropriate parameters. Here's an example of how to do it using Node and the node-fetch module:

import fs from "fs";

const API_KEY = "<your_api_key>";
const API_ENDPOINT = "https://konbert.com/api/v1/convert";
const inputFilePath = "<input_file_path>";
const outputFilePath = "<output_file_path>";

const convertFile = async () => {
  const input = fs.createReadStream(inputFilePath);
  const formData = new FormData();
  formData.append("input[file]", input);
  formData.append("input[format]", "json");
  formData.append("output[format]", "csv");
  formData.append("output[options][delimiter]", ",");
  formData.append("sync", "true");

  const response = await fetch(API_ENDPOINT, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${API_KEY}`,
    },
    body: formData,
  });

  if (!response.ok) {
    throw new Error("Failed to convert file");
  }

  const result = await response.text();

  fs.writeFileSync(outputFilePath, result);

  console.log("File converted successfully");
};

convertFile().catch(console.error);

In this example, we're reading the input file from the file system using fs.createReadStream and passing it to the FormData object. We're specifying the input format as JSON and the output format as CSV. We've also set the output delimiter to , to separate the values in the CSV file.

We're making the HTTP request using fetch and await, and checking if the response is OK. If the response is OK, we're converting the result to text format using response.text(), and writing it to the output file using fs.writeFileSync.

The convertFile function returns a promise, which we're catching using catch and logging any errors to the console.