Skip to main content

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

Request Headers

HeaderTypeRequiredDescription
AuthorizationstringYesBearer token with your API key
Content-TypestringYesMust be application/json

Request Body

ParameterTypeRequiredDescription
textstringYesThe text prompt to optimize
target_modelstringNoTarget model for optimization (default: “gpt4o”)
optimization_modelstringNoOptimization 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

FieldTypeDescription
messagestringThe optimized text prompt
tokens_reducedintegerNumber of tokens saved by optimization

Code Examples

  • Python
  • JavaScript
  • cURL
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"
}