Lokalise API Client for Python
The official Python interface for the Lokalise API v2, designed to interact with your Lokalise translation projects programmatically. It provides a comprehensive set of functionalities to manage projects, keys, translations, and more. Currently at version 4.0.4, the library maintains an active release cadence, frequently adding new features and ensuring compatibility with the latest Lokalise API changes.
Common errors
-
AttributeError: 'LokaliseProject' object has no attribute 'get'
cause You are trying to access fields on a Pydantic model (introduced in v4.x) using dictionary-like syntax (e.g., `project.get('name')` or `project['name']`), which was common in v3.x.fixAccess model fields directly as attributes: `project.name`, `project.project_id`, etc. (e.g., `print(project.name)`). -
ImportError: cannot import name 'LokaliseClient' from 'lokalise'
cause The main client class `LokaliseClient` is not directly under the top-level `lokalise` package in v4.x.fixUpdate your import statement to `from lokalise.client import LokaliseClient`. -
httpx.HTTPStatusError: Client error '401 Unauthorized' for url 'https://api.lokalise.com/api/v2/projects'
cause The provided API token is invalid, expired, or does not have sufficient permissions for the requested operation.fixDouble-check your `LOKALISE_API_TOKEN` environment variable for correctness. Ensure it has the necessary read/write permissions for the resources you are trying to access in Lokalise.
Warnings
- breaking Version 4.x introduced significant breaking changes from v3.x. The most notable change is the adoption of `pydantic` v2 for API responses, meaning methods now return strongly-typed Pydantic models instead of raw dictionaries. Additionally, the underlying HTTP client switched from `requests` to `httpx`.
- gotcha By default, API calls that return a list of items (e.g., `client.projects().list()`) will only return the first page of results. Automatic pagination is disabled by default for performance and control.
- gotcha API tokens should be handled securely. Hardcoding tokens directly into your script is a security risk, especially in production environments.
Install
-
pip install python-lokalise-api
Imports
- LokaliseClient
from lokalise import LokaliseClient
from lokalise.client import LokaliseClient
Quickstart
import os
from lokalise.client import LokaliseClient
# Get your Lokalise API token from environment variables for security
API_TOKEN = os.environ.get('LOKALISE_API_TOKEN', '')
if not API_TOKEN:
print("Error: LOKALISE_API_TOKEN environment variable not set.")
print("Please set it (e.g., export LOKALISE_API_TOKEN='YOUR_TOKEN') and retry.")
else:
try:
client = LokaliseClient(API_TOKEN)
# List projects (auto_pagination is False by default, returns first page)
projects = client.projects().list()
if projects:
print(f"Found {len(projects)} projects on the first page:")
for project in projects:
print(f" ID: {project.project_id}, Name: {project.name}")
else:
print("No projects found or API token is invalid.")
except Exception as e:
print(f"An error occurred: {e}")