Node.js Examples

Here are some examples of how to use the Konbert API with Node.js. These examples demonstrate different conversion scenarios and options.

Convert JSON file to PostgreSQL

This example shows how to convert a JSON file to PostgreSQL format using the Konbert API.

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 formData = new FormData();
formData.append("input[file]", fs.createReadStream(inputFilePath));
formData.append("input[format]", "json");
formData.append("input[options][flatten_objects]", "true");
formData.append("output[format]", "sql");
formData.append("output[options][create_table]", "true");
formData.append("output[options][syntax]", "postgres");
formData.append("output[options][table_name]", "mytable");
formData.append("sync", "true");
async function convertFile() {
try {
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 arrayBuffer = await response.arrayBuffer();
fs.writeFileSync(outputFilePath, Buffer.from(arrayBuffer));
console.log("File converted successfully");
} catch (error) {
console.error("Error:", error);
}
}
convertFile();

Convert CSV URL to Parquet

This example demonstrates how to convert a CSV file from a URL to Parquet format.

import fetch from "node-fetch";
import fs from "fs";
const API_KEY = "<your_api_key>";
const API_ENDPOINT = "https://konbert.com/api/v1/convert";
const outputFilePath = "<output_file_path>";
async function convertCsvUrlToParquet() {
const body = JSON.stringify({
input: {
http: {
url: "https://example.com/data.csv"
},
format: "csv",
options: {
delimiter: ","
}
},
output: {
format: "parquet"
},
sync: true
});
try {
const response = await fetch(API_ENDPOINT, {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json"
},
body: body
});
if (!response.ok) {
throw new Error("Failed to convert file");
}
const arrayBuffer = await response.arrayBuffer();
fs.writeFileSync(outputFilePath, Buffer.from(arrayBuffer));
console.log("File converted successfully");
} catch (error) {
console.error("Error:", error);
}
}
convertCsvUrlToParquet();