Transifex

  • Documentation
  • API 3.0 (Preview)
  • Sample API Calls

Sample API Calls

Uploading New Strings and Updating Existing Strings

When pushing strings to Transifex using API V3, please note the following:

  1. If a resource does not exist, then create one first (without pushing data) using this API Endpoint: https://rest.api.transifex.com/resources.
  2. Once the resource is created, you can then push data either by uploading a file, or by sending JSON encoded strings. The same endpoint is used in both cases: https://rest.api.transifex.com/resource_strings_async_uploads
  3. Whether updating existing strings, or adding new ones the same endpoint is used.

The following code in Python demonstrates how to incorporate these API calls to add new content to Transifex or update existing content. There are several converters available on the web that can convert this code to other programming languages.

import requests
import json
import sys
# uploading and updating files in Transifex using API V3.0
organization_slug = 'Transifex-93'
project_slug = 'APIV30TestC'
resource_slug = 'ExamplePO'
resource_name = 'Example.PO'
sourceFilePath = '/Users/anthonydimitrokalis/Documents/Data/Samples/FileformatsExamples/example.po'
resource_id = 'o:' + organization_slug + ":p:" + project_slug + ":r:" + resource_slug
project_id = "o:" + organization_slug + ":p:" + project_slug 
apitoken = 'Bearer apitoken'

def putSourceStrings_File():
    #upload file to an existing resource. This script is the same whether creating a new file or updating an existing one
    url = "https://rest.api.transifex.com/resource_strings_async_uploads"
    payload = {'resource': resource_id}
    # note let the request determine content-type otherwise 400 error occurs
    files = {('content', open(sourceFilePath,'rb'))}
    headers = {'Authorization': apitoken,}
    response = requests.request("POST", url, headers=headers, data=payload, files=files)
    return response.status_code;

def putSourceStrings_JSONStrings():
    #upload JSON encoded strings to an existing resource. Note: The contents sent with this call overwrite the strings in Transifex
    url = "https://rest.api.transifex.com/resource_strings_async_uploads"
    payload = {'resource': resource_id}
    # note let the request determine content-type otherwise a 400 error occurs
    f1 = open(sourceFilePath, 'r')
    files = json.loads(f1)
    headers = {'Authorization': apitoken,}
    response = requests.request("POST", url, headers=headers, data=payload, files=files)
    return response.status_code;

def getFileInfo():
    # 1. Determine if resource already exists
    url = "https://rest.api.transifex.com/resources/" + resource_id
    payload = {}
    headers = {'Authorization': apitoken,}
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.status_code;

def CreateNewResource():
    # Create New Resource
    url = "https://rest.api.transifex.com/resources" 
    payload = {
    "data": {
        "attributes": {
            "accept_translations": True,
            "categories": [
                "category_1",
                "category_2"
            ],
            "i18n_type": "PO",
            "name": resource_name,
            "priority": "normal",
            "slug": resource_slug
        },
        "relationships": {
            "project": {
                "data": {
                    "id": project_id,
                    "type": "projects"
                }
            }
        },
        "type": "resources"
        }
    }
    headers = {
        'Content-Type': 'application/vnd.api+json',
        'Authorization': apitoken,
    }
    response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
    return response.status_code;

def errorHandler(responseText):
    print (responseText)
    sys.exit("Error")  

responseCode = getFileInfo();
if responseCode == 404: 
    responseCode2 = CreateNewResource();
    if responseCode2 != 201:
        errorHandler('Creating New Resource: ' + response.text.encode('utf8'));
elif responseCode == 200:      
    #file exists
    # either push new content or update existing content by uploading a file
    responseCode2 = putSourceStrings_File();
    if responseCode2 != 202:
        errorHandler('Pushing Content via File: ' + response.text.encode('utf8'));
    else:
        print ('File was successfully pushed')
    # or push new content or update existing content by pushing
    # a JSON encoded string.
    responseCode2 = putSourceStrings_File();
    if responseCode2 != 202:
        errorHandler('Pushing Content via JSON encoded strings: ' + response.text.encode('utf8'));
    else:
        print ('JSON encoded strings were successfully pushed')
else:
    errorHandler('Retrieving Resource Info: ' + response.text.encode('utf8'));

In the above example, the organization, project and resource slugs can be obtained from the URL of the resource when opened in Transifex as can be seen below:

slug.png#asset:8247