Skip to content

First Request

Languages

Below are examples of requests to an API using different programming languages:

NodeJS - Request

const fs = require("fs");
const axios = require("axios");
const FormData = require("form-data");

const apiHost = "https://api.lettria.com";
const apiRoute = apiHost + "/parse";

const documentFileName = "file.html";
const documentFileLocation = "./data/";

const jwtTokenFile = "jwt_token.txt";
const jwtToken = fs.readFileSync(jwtTokenFile, "utf-8").trim();

const formData = new FormData();
formData.append(
	"file",
	fs.createReadStream(documentFileLocation + documentFileName)
);

axios
	.post(apiRoute, formData, {
		headers: {
			Authorization: "LettriaProKey " + jwtToken,
			...formData.getHeaders(),
		},
	})
	.then((response) => {
		const chunks = response.data.chunks;
		chunks.forEach((chunk) => {
			console.log(JSON.stringify(chunk, null, 4));
		});
	})
	.catch((error) => {
		console.error("Error:", error);
	});

Python - Requests

In this example, the API key is stored in the jwt_token.txt file (you might use a .env and dotenv as well).

import json
import requests

api_host = 'https://api.lettria.com'
api_route = api_host + '/parse'

document_file_name = 'file.html'
document_file_location = './data/'

jwt_token_file = 'jwt_token.txt'
jwt_token = open(jwt_token_file, 'r').read()

chunks = []
with open(document_file_location + document_file_name, 'rb') as file:
    response = requests.post(
        api_route,
        files=[('file', ( document_file_name, file))],
        headers={"Authorization":"LettriaProKey " + jwt_token}
    ).json()
    chunks = response['chunks']

for chunk in chunks:
    print(json.dumps(chunk, indent=4))

Next steps