Optimize Text
Optimize long text prompts into shorter, token-efficient versions.
This is the main endpoint for text optimization. It accepts any text input and returns an optimized version with reduced token count.
Endpoint
POST /v1/optimization/optimize
| Header | Type | Required | Description |
|---|
Authorization | string | Yes | Bearer token with your API key |
Content-Type | string | Yes | Must be application/json |
Request Body
| Parameter | Type | Required | Description |
|---|
text | string | Yes | The text prompt to optimize |
target_model | string | No | Target model for optimization (default: “gpt4o”) |
optimization_model | string | No | Optimization model to use (default: “latest”) |
Example Request
curl -X POST "https://api.sqwish.ai/v1/optimization/optimize" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $SQWISH_API_KEY" \
-d '{
"text": "Write a detailed blog post about recursion in programming.",
"target_model": "gpt4o",
"optimization_model": "latest"
}'
Response
The API returns a JSON response with the optimized text and token reduction information.
Success Response (200)
{
"message": "Write a blog post about recursion in programming.",
"tokens_reduced": 8
}
Response Fields
| Field | Type | Description |
|---|
message | string | The optimized text prompt |
tokens_reduced | integer | Number of tokens saved by optimization |
Code Examples
import requests
import os
headers = {
"Authorization": f"Bearer {os.environ.get('SQWISH_API_KEY')}",
"Content-Type": "application/json"
}
payload = {
"text": "Write a detailed blog post about recursion in programming.",
"target_model": "gpt4o",
"optimization_model": "latest"
}
response = requests.post("https://api.sqwish.ai/v1/optimization/optimize", json=payload, headers=headers)
data = response.json()
print("Optimized message:", data["message"])
print("Tokens Saved:", data["tokens_reduced"])
Error Responses
400 Bad Request
{
"error": "Invalid text parameter"
}
401 Unauthorized
{
"error": "Invalid API key"
}
429 Rate Limit Exceeded
{
"error": "Rate limit exceeded. Please try again later."
}
500 Internal Server Error
{
"error": "Internal server error"
}