Skip to content

First Request

Languages

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

NodeJs - Request


Python - Requests

import requests
import json

url = "https://api.lettria.com/parse"

files = [
	(
        'file',
        (
            'my_file.txt',              # file name with extension
            open('my_file.txt', 'rb'),  # file bytes
            'application/text'          # file MIME type
        )
    )
]
data = {
	"name": filename,
	"extension" : extension,
	"options": "{'type': 'text'}"
}
headers = { 'Authorization': 'LettriaProKey API_TOKEN' }

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

print(response.text)

cURL


Examples

Get document chunks

Get all text chunks from a document.

import requests

def getDocumentTextChunks(filepath, mimetype):
	filewithext = filepath.split('/')[-1]
	extension   = filepath.split('.')[-1]
	filename	= filewithext[:-(len(extension)+1)]
	filebytes 	= open(filepath, 'rb')

	rq = requests.post(
		'https://api.lettria.com/parse',
		files = [
			('file', (filewithext, filebytes, mimetype))
		],
		data = {
			'name': filename,
			'extension' : extension,
			'options': "{'type': 'text'}"
		},
		headers = {
			'Authorization': 'LettriaProKey $API_TOKEN',
		})
	res = rq.json()

	chunks = [chunk['content'] for chunk in res['chunks'] if chunk['type'] == 'text']

	return chunks

Next step