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/",
	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/"

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/' \
--header 'Authorization: LettriaProKey ${API_TOKEN}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "documents": [
       "First document to analyse"
    ]
}'

Examples

Get locations

Get all locations from a document such as cities, countries, etc.

import axios from "axios";

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

const getDocumentLocations = async (document) => {
	const data = await getLettriaAnalysis(document);
	let locations = [];
	data?.forEach((documentData) => {
		documentData?.sentences?.forEach((sentenceData) => {
			locations.push(
				sentenceData?.detail?.filter((detail) =>
					detail?.meaning?.find(
						(meaning) => meaning?.sub === "created_location"
					)
				)
			);
		});
	});
	return locations.flat();
};

Next steps