Recipe for Success: Creating, Updating, and Deleting Resources with the Transifex Python SDK

Hello, fellow code enthusiasts! :woman_cook::man_cook: Welcome back to ‘Script Chef,’ where we whip up coding recipes that bring your projects to life. Today, we’re stirring up a treat straight from the Transifex kitchen to help you master the art of resource management with the Transifex Python SDK.

In this guide, we’ll walk through a recipe for working with Transifex resources using the Transifex Python SDK. You’ll learn how to create a new resource, upload content, and delete it when necessary. This step-by-step process will help you master the basics of resource management, all from the comfort of your Python environment!

Ingredients You’ll Need

  1. The Transifex Python SDK: Install the SDK directly from PyPI by running:
pip install transifex-python
  1. API Token: Ensure you have a valid Transifex API token. This token allows the SDK to interact with your Transifex account.
  2. Organization and Project Slugs: Identify the organization and project slugs where your resources will live.

Step 1: Authenticate with Transifex API

To get started, import the SDK and set up authentication using your API token:

from transifex.api import transifex_api

# Replace with your actual API token
transifex_api.setup(auth="<YOUR-API-TOKEN>")

Step 2: Retrieve Organization and Project

Once authenticated, you can access your organization and project information. Use the appropriate slugs to locate the project you want to work with:

organization = transifex_api.Organization.get(slug="<YOUR-ORG-SLUG>")
project = organization.fetch('projects').get(slug="<YOUR-PROJECT-SLUG>")

Step 3: Create a New Resource

Define your resource format by specifying the i18n format you’ll use. In this example, we’ll use “KEYVALUEJSON,” commonly used for JSON files in localization.

# Specify the format
i18n_format = transifex_api.i18n_formats.get(
    organization=organization,
    name="KEYVALUEJSON")

# Create the resource
resource = transifex_api.Resource.create(
    name="<NEW-RESOURCE-NAME>",
    slug="<NEW-RESOURCE-SLUG>",
    i18n_format=i18n_format,
    project=project,
)

Tip: Replace <NEW-RESOURCE-NAME> and <NEW-RESOURCE-SLUG> with unique identifiers for your resource. Resource names and slugs must be unique within a project.

Step 4: Upload Content

To upload content to your newly created resource, define the content and use the ResourceStringsAsyncUpload method:

content = '{ "key": "value" }'
transifex_api.ResourceStringsAsyncUpload.upload(
  resource=resource, content=content
)

This command uploads the JSON-formatted string directly into the resource. Transifex handles the heavy lifting, parsing and adding the content to your project.

Step 5: Delete the Resource

When it’s time to clean up, deleting a resource is just as simple as creating it. Call the delete method on the resource:

resource.delete()

That’s It! You’re Done

With these simple steps, you’ve successfully created, updated, and deleted a resource in Transifex using Python. Feel free to experiment with different content formats and explore further capabilities of the Transifex SDK.


We hope you enjoyed this recipe from ‘Script Chef’! If you have any questions or need further assistance, don’t hesitate to contact our community or support team. Happy coding!

3 Likes