The backend server for ToneSlider, a tone adjustment tool that allows users to rewrite text in varying levels of formality using a slider. This backend connects with the Mistral AI API to process text tone, uses Upstash Redis for response caching, and is designed to be fast and efficient.
The backend is deployed at:
https://toneslider.up.railway.app
- Node.js with Express.js – REST API server
- Upstash Redis – Caching layer for performance
- Mistral AI API – Language model for tone transformation
- CORS – For secure cross-origin requests
- dotenv – Environment variable management
- Accepts text and tone level, returns rewritten text in the desired tone
- Uses caching to reduce API calls and improve response time
- Error handling and graceful fallbacks
- Environment-variable-driven setup for secure credentials
git clone https://github.com/yourusername/toneslider-backend.git
cd toneslider-backendnpm installPORT=3000
MISTRAL_API_KEY=your_mistral_api_key
REDIS_URL=your_upstash_redis_urlnpm startAdjusts the tone of a given piece of text based on the tone level.
Request Body:
{
"text": "Your original text here.",
"toneLevel": 70
}Response
{
"content": "Your text rewritten in a casual tone."
}
To improve performance and reduce redundant API calls to Mistral, this backend uses Upstash Redis for caching.
- Each request to adjust tone is cached using a unique key format:
- If a cached result exists, it is returned instantly without making an API call.
- Cached data has a TTL (Time To Live) of 3600 seconds (1 hour).
- Reduces latency for repeated requests
- Lowers Mistral API usage and costs
- Provides a smoother user experience
This backend handles errors gracefully to ensure users aren't left stuck:
- API Failures: If the Mistral API fails or returns an error, the server returns the original input text with an error message.
- Redis Failures: If Redis is unreachable or fails to connect, the system continues to function but skips caching.
- Invalid Input: Requests without valid
textortoneLevelare ignored with appropriate client-side validation. - Frontend Feedback: Using
sonnertoasts, users are alerted when: - Tone adjustment fails
- An undo or redo operation is performed
- Text is reset to its original state
We welcome contributions to improve ToneSlider! If you'd like to help:
- 🍴 Fork the repository
- 🛠️ Create a new branch (
git checkout -b feature-name) - 💾 Make your changes and commit (
git commit -m "Add some feature") - 🚀 Push to your branch (
git push origin feature-name) - 🔁 Open a Pull Request
- Write clean, readable code with comments if needed.
- Ensure all tests pass before submitting.
- Keep commits atomic and descriptive.
- For new features or bug fixes, please open an issue first to discuss the idea.
📦 Frontend Setup:
To view and interact with the full ToneSlider application, please refer to the https://github.com/isid555/ToneSlider and follow its setup instructions.
📫 For major changes or feature discussions, feel free to reach out or open an issue. Let’s build something amazing together!