# Sample API 3.0 Code in Python: Pushing strings to Transifex

**URL:** <https://community.transifex.com/t/sample-api-3-0-code-in-python-pushing-strings-to-transifex/2097>\
**Category:** Developers\
**Created:** [November 30, 2020, 12:46pm UTC](https://community.transifex.com/t/sample-api-3-0-code-in-python-pushing-strings-to-transifex/2097 "2020-11-30T12:46:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthonydim](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@anthonydim](https://community.transifex.com/u/anthonydim)\
**Post date:** [November 30, 2020, 12:46pm UTC](https://community.transifex.com/t/sample-api-3-0-code-in-python-pushing-strings-to-transifex/2097/1 "2020-11-30T12:46:55Z")

</div>

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](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](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.

> Blockquote

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](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](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/](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](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 succesfully 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 succesfully pushed’)  
else:  
errorHandler('Retrieving Resource Info: ’ + response.text.encode(‘utf8’));

> Blockquote

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:

![image](https://canada1.discourse-cdn.com/flex030/uploads/transifex/original/2X/9/973ee0b8a72f13c1dad419ac01d103e024a306c6.png)
