Skip to content

First Request

Languages

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

NodeJs - Request

var request = require("request");
var options = {
	method: "POST",
	url: "https://api.lettria.com/nls/classification",
	headers: {
		Authorization: "LettriaProKey API_TOKEN",
		"Content-Type": "application/json",
	},
	body: JSON.stringify({
		documents: ["hello"],
	}),
};
request(options, function (error, response) {
	if (error) throw new Error(error);
	console.log(response.body);
});

Python - Requests

import requests
import json

url = "https://api.lettria.com/nls/classification"

payload = json.dumps({
  "documents": [
    "hello"
  ]
})
headers = {
  'Authorization': 'LettriaProKey API_TOKEN',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

cURL

curl --location --request POST 'https://api.lettria.com/nls/classification' \
--header 'Authorization: LettriaProKey ${API_TOKEN}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "documents": [
       "First document to analyse"
    ]
}'

Examples

Get matched labels

Get all the labels that match the document.

import axios from "axios";

const getLettriaAnalysis = async (document) => {
	const response = await axios.post(
		"https://api.lettria.com/nls/classification",
		{
			documents: [document],
		},
		{
			headers: {
				Authorization: "LettriaProKey $API_TOKEN",
				"Content-Type": "application/json",
			},
		}
	);
	return response.data;
};

const getMatchedLabels = async (document) => {
	const response = await getLettriaAnalysis(document);
	let labels = [];

	response?.forEach((documentData) => {
		documentData?.data?.forEach((matchData) => {
			labels.push(matchData?.label);
		});
	});
	labels = labels.flat();
	return labels;
};

Next steps