Phrase Strings API Python Client

3.19.0 · active · verified Fri Apr 17

The `phrase-api` library provides a Python client for interacting with the Phrase Strings API. It allows programmatic access to translation, localization, and internationalization resources. As a generated client, it frequently updates (often monthly) to reflect the latest API specification, with version `3.19.0` being the current stable release.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to configure the client with an API token, create an API instance, and fetch a list of locales for a given project. It uses environment variables for sensitive data like API tokens and project IDs.

import os
import phrase_api
from phrase_api.rest import ApiException
from pprint import pprint

# Configure API key authorization
configuration = phrase_api.Configuration()
configuration.api_key['api_token'] = os.environ.get('PHRASE_API_TOKEN', 'YOUR_PHRASE_API_TOKEN')
# Optionally, uncomment for bearer token prefix:
# configuration.api_key_prefix['api_token'] = 'Bearer'

# Create an instance of the API client
api_client = phrase_api.ApiClient(configuration)

# Create an instance of a specific API (e.g., LocalesApi)
locales_api = phrase_api.LocalesApi(api_client)

try:
    # List locales for a project
    project_id = os.environ.get('PHRASE_PROJECT_ID', 'YOUR_PHRASE_PROJECT_ID') # Replace with your project ID
    
    # Optional: Two-Factor-Authentication token, if required for your account/action
    x_phrase_app_otp = os.environ.get('PHRASE_OTP_TOKEN') 
    
    # Fetching the first page of locales, 25 per page
    api_response = locales_api.locales_list(
        project_id,
        page=1, 
        per_page=25, 
        x_phrase_app_otp=x_phrase_app_otp
    )
    
    print("Successfully listed locales:")
    pprint(api_response)

except ApiException as e:
    print(f"Exception when calling LocalesApi->locales_list: {e}")
    if e.status == 401:
        print("Hint: Check your PHRASE_API_TOKEN and ensure it has necessary permissions.")
    elif e.status == 404:
        print("Hint: Check your PHRASE_PROJECT_ID and ensure it exists and is accessible.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →