Text & Transforms¶
14 endpoints for text manipulation, data transformation, and formatting tasks.
| Endpoint | Purpose |
|---|---|
POST /v1/diff |
Compute a unified diff between two text strings |
POST /v1/slug |
Convert text to a URL-friendly slug |
POST /v1/word-count |
Count words, characters, sentences, and paragraphs |
POST /v1/sql-format |
Format and pretty-print a SQL query |
POST /v1/text-escape |
Escape or unescape text (shell, regex, JSON, SQL, XML) |
POST /v1/text-truncate |
Truncate text to a maximum length |
POST /v1/code-format |
Format source code (Python, JSON, XML, HTML, SQL) |
POST /v1/liquid-render |
Render a Liquid template with variables |
POST /v1/json-transform |
Transform JSON with a JMESPath expression |
POST /v1/math-eval |
Evaluate a mathematical expression |
POST /v1/date-format |
Parse, format, and convert dates between timezones |
POST /v1/json-flatten |
Flatten or unflatten a nested JSON object |
POST /v1/array-ops |
Sort, unique, intersect, diff, union, chunk arrays |
POST /v1/number-format |
Format a number (decimal, currency, percent, scientific) |
Python SDK Examples¶
Compute a unified diff¶
from toolkitapi import DevTools
original = "Hello world\nThis is line two\nThis is line three"
modified = "Hello world\nThis is line 2\nThis is line three\nNew line added"
with DevTools(api_key="tk_...") as dt:
result = dt.diff(original, modified)
print(result["unified"]) # Unified diff output
print(result["added"]) # Number of added lines
print(result["removed"]) # Number of removed lines
Slugify text¶
from toolkitapi import DevTools
with DevTools(api_key="tk_...") as dt:
result = dt.slug("Hello World! This is a Test.", separator="-", max_length=30)
print(result["slug"]) # "hello-world-this-is-a-test"
Count words and characters¶
from toolkitapi import DevTools
text = "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs."
with DevTools(api_key="tk_...") as dt:
result = dt.word_count(text)
print(result["words"]) # 16
print(result["characters"]) # 86 (with spaces)
print(result["sentences"]) # 2
print(result["paragraphs"]) # 1
print(result["read_time"]) # "< 1 min"
Format a SQL query¶
from toolkitapi import DevTools
sql = "select u.id,u.name,o.total from users u inner join orders o on u.id=o.user_id where u.active=1 order by o.total desc"
with DevTools(api_key="tk_...") as dt:
result = dt.sql_format(sql, indent=2, uppercase=True)
print(result["formatted"])
# SELECT
# u.id,
# u.name,
# o.total
# FROM users u
# INNER JOIN orders o ON u.id = o.user_id
# WHERE u.active = 1
# ORDER BY o.total DESC
Escape text for different contexts¶
from toolkitapi import DevTools
with DevTools(api_key="tk_...") as dt:
# Escape for shell
result = dt.text_escape("Hello; rm -rf /", context="shell")
print(result["escaped"])
# Escape for SQL
result = dt.text_escape("O'Brien's café", context="sql")
print(result["escaped"])
# Unescape JSON
result = dt.text_escape(r'Hello \"world\"', context="json", unescape=True)
print(result["escaped"])
Truncate text¶
from toolkitapi import DevTools
long_text = "The quick brown fox jumps over the lazy dog and continues running."
with DevTools(api_key="tk_...") as dt:
result = dt.text_truncate(long_text, max_length=30, word_boundary=True)
print(result["truncated"]) # "The quick brown fox jumps..."
print(result["truncated"]) # Always ends at a word boundary
Format source code¶
from toolkitapi import DevTools
messy_python = "def hello(name): print( 'hello '+name )"
with DevTools(api_key="tk_...") as dt:
result = dt.code_format(messy_python, language="python")
print(result["formatted"])
Render a Liquid template¶
from toolkitapi import DevTools
template = "Hello, {{ name }}! You have {{ count }} new {{ count | pluralize: 'message', 'messages' }}."
with DevTools(api_key="tk_...") as dt:
result = dt.liquid_render(
template,
variables={"name": "Alice", "count": 3},
)
print(result["output"]) # "Hello, Alice! You have 3 new messages."
Transform JSON with JMESPath¶
from toolkitapi import DevTools
data = {
"people": [
{"name": "Alice", "age": 30, "active": True},
{"name": "Bob", "age": 25, "active": False},
{"name": "Carol", "age": 35, "active": True},
]
}
with DevTools(api_key="tk_...") as dt:
result = dt.json_transform(data, "people[?active].name")
print(result["result"]) # ["Alice", "Carol"]
Evaluate a math expression¶
from toolkitapi import DevTools
with DevTools(api_key="tk_...") as dt:
result = dt.math_eval("round(sqrt(price * quantity), 2)", variables={"price": 19.99, "quantity": 7})
print(result["result"]) # 11.83
Format and convert dates¶
from toolkitapi import DevTools
with DevTools(api_key="tk_...") as dt:
result = dt.date_format(
"2024-06-15 09:30:00",
input_format="%Y-%m-%d %H:%M:%S",
output_format="%B %d, %Y at %I:%M %p",
from_timezone="US/Eastern",
to_timezone="UTC",
)
print(result["formatted"]) # "June 15, 2024 at 01:30 PM"
Flatten a nested JSON object¶
from toolkitapi import DevTools
nested = {
"user": {
"name": "Alice",
"address": {
"city": "London",
"postcode": "SW1A 1AA",
},
}
}
with DevTools(api_key="tk_...") as dt:
result = dt.json_flatten(nested, separator=".")
print(result["data"])
# {
# "user.name": "Alice",
# "user.address.city": "London",
# "user.address.postcode": "SW1A 1AA"
# }
# Unflatten it back
result = dt.json_flatten(result["data"], direction="unflatten")
print(result["data"])
Array operations¶
from toolkitapi import DevTools
with DevTools(api_key="tk_...") as dt:
# Get unique values
result = dt.array_ops("unique", [[1, 2, 2, 3, 3, 4]])
print(result["result"]) # [1, 2, 3, 4]
# Intersect two arrays
result = dt.array_ops("intersect", [[1, 2, 3, 4], [3, 4, 5, 6]])
print(result["result"]) # [3, 4]
# Chunk into groups of 3
result = dt.array_ops("chunk", [[1, 2, 3, 4, 5, 6, 7]], options={"chunk_size": 3})
print(result["result"]) # [[1, 2, 3], [4, 5, 6], [7]]
Format a number¶
from toolkitapi import DevTools
with DevTools(api_key="tk_...") as dt:
result = dt.number_format(1234567.89, locale="en_US", format_type="currency", currency="USD")
print(result["formatted"]) # "$1,234,567.89"
result = dt.number_format(0.1234, format_type="percent", precision=1)
print(result["formatted"]) # "12.3%"
Tip
Combine json-transform (JMESPath) with json-flatten to reshape and normalise complex API responses before inserting them into a database or sending to a downstream service.