survos / lecto-php
PHP client for the Lecto Translation API (v2 LLM translation, batching, glossaries). Works with any PSR-18 HTTP client: Symfony HttpClient, Guzzle, ...
Fund package maintenance!
Requires
- php: ^8.4
- ext-json: *
- php-http/discovery: ^1.20
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.9
- nyholm/psr7: ^1.8
- phpunit/phpunit: ^12.0 || ^13.0
- symfony/http-client: ^7.3 || ^8.0
Suggests
- guzzlehttp/guzzle: PSR-18 client (GuzzleHttp\Client) with its own PSR-17 factories (GuzzleHttp\Psr7\HttpFactory)
- symfony/http-client: PSR-18 client (Symfony\Component\HttpClient\Psr18Client), with nyholm/psr7
Provides
None
Conflicts
None
Replaces
None
README
A PHP client for the Lecto Translation API, including the LLM-powered v2 endpoint: context, per-text notes, formality and per-request glossaries.
- Any PSR-18 HTTP client. Symfony HttpClient, Guzzle 7, or anything else;
LectoClient::create()finds the one you have installed. - Typed requests and responses. Readonly DTOs, enums, PHP 8.4.
- Batching that fits your plan.
Batcherpacks any number of texts and target languages into as few requests as the plan's limits allow, and gives the results back merged and in order. - Typed errors. Rate limits come with
retryAfter; Lecto's per-field error details are kept.
Framework integration (configuration, caching, retries, a translation engine interface shared with DeepL, Google and LibreTranslate) lives in survos/translator-bundle.
composer require survos/lecto-php symfony/http-client nyholm/psr7
# or: composer require survos/lecto-php guzzlehttp/guzzle
Quick start
use Survos\Lecto\LectoClient; $lecto = LectoClient::create($_ENV['LECTO_API_KEY']); echo $lecto->translateText('Hello, world!', to: 'es'); // ¡Hola, mundo!
Translate many texts, with context
use Survos\Lecto\Model\{Dictionary, Formality, TranslateRequest}; $response = $lecto->translate(new TranslateRequest( texts: ['Checkout', 'Add to cart'], to: ['de', 'es'], from: 'en', context: 'Fashion e-commerce checkout flow', // applies to every text notes: ['Button on the cart page', 'Button on the product page'], // one per text formality: Formality::Less, dictionaries: [new Dictionary('en', 'de', ['Cart' => 'Warenkorb', 'Checkout' => 'Kasse'])], )); $response->texts('de'); // ['Zur Kasse', 'In den Warenkorb'] $response->translatedCharacters; // what Lecto counted
Keep names as they are
Machine translation likes to turn "Bill Clinton" into "Cuenta Clinton". A glossary entry that maps a term to itself keeps it:
Dictionary::keep('en', 'es', ['Bill Clinton', 'George Bush'])
Batches larger than one request
Each Lecto plan caps the characters and the target languages in one request. Batcher splits the work to fit,
keeping each text's note with it, and merges the answers:
use Survos\Lecto\{Batcher, Plan}; $response = (new Batcher($lecto, Plan::Sandbox))->translate(new TranslateRequest( texts: $thousandsOfTitles, to: ['es', 'de', 'fr', 'it'], from: 'en', )); $response->texts('fr'); // in the same order as $thousandsOfTitles $response->requests; // how many HTTP requests it took
Lecto bills a fixed 100 characters per v2 request, so fuller requests cost less. Batcher::requests() yields the
requests without sending them, for parallel sending within Plan::maxParallelRequests().
| Plan | Characters per request | Targets per request | Parallel requests | Characters per month |
|---|---|---|---|---|
PayAsYouGo |
1,000 | 2 | 2 | (credits) |
Flexible |
1,000 | 2 | 2 | 1M |
Sandbox |
2,000 | 3 | 4 | 14M |
Growth |
5,000 | 4 | 6 | 200M |
Business |
7,500 | 5 | 8 | 750M |
(From lecto.ai/pricing, November 2026. Explicit limits: new Batcher($lecto, maxCharacters: 3000, maxTargets: 3).)
Other endpoints
$lecto->detect('Jó napot kívánok', 'Dobrý den')->detectedLanguages; // ['hu', 'cs'] $lecto->languages(); // list<Language> $lecto->account(); // plan, usage, usageLimit, period // v1 JSON translation: values are translated, keys and structure kept. use Survos\Lecto\Model\JsonTranslateRequest; $lecto->translateJson(new JsonTranslateRequest($document, to: ['es'], from: 'en', protectedKeys: ['team.name'])) ->document('es');
Choosing the HTTP client
// Symfony HttpClient: Psr18Client is the client and both PSR-17 factories. $lecto = new LectoClient(new \Symfony\Component\HttpClient\Psr18Client(), $key); // Guzzle 7 $factory = new \GuzzleHttp\Psr7\HttpFactory(); $lecto = new LectoClient(new \GuzzleHttp\Client(['timeout' => 60]), $key, $factory, $factory); // Through RapidAPI $lecto = LectoClient::rapidApi($psr18Client, $rapidApiKey);
The client sends one request per call and does not retry or cache. Wrap the HTTP client for retries
(Symfony's RetryableHttpClient, a Guzzle retry middleware), or use translator-bundle, which adds both.
Errors
| Exception | When |
|---|---|
Exception\InvalidRequestException |
Refused before sending (e.g. notes not parallel to texts) |
Exception\AuthenticationException |
401: missing or invalid key |
Exception\RateLimitException |
429; $e->retryAfter seconds when Lecto sends Retry-After |
Exception\ApiException |
Any other error status; $e->details holds Lecto's per-field messages |
All extend Exception\LectoException.
Examples and tests
examples/ runs against the live API (LECTO_API_KEY=... php examples/batch.php). Tests run offline:
composer install && vendor/bin/phpunit
MIT licensed.