Need to understand the emotional tone of text? This guide covers everything you need to know about sentiment analysis via API, including polarity scoring, emotion detection, and implementation examples.
What is Sentiment Analysis?
Sentiment analysis (also called opinion mining) is a natural language processing technique that identifies and extracts the emotional tone from text. It determines whether the expressed opinion is positive, negative, or neutral.
For example, "This product is amazing!" has positive sentiment, while "Terrible experience, never again" has negative sentiment.
Types of Sentiment
Sentiment can be classified at different levels:
| Type | Description | Example |
|---|---|---|
| Positive | Favorable, happy, satisfied | "Love this! Highly recommend." |
| Negative | Unfavorable, upset, dissatisfied | "Worst purchase ever made." |
| Neutral | Objective, factual, no emotion | "The product arrived on Monday." |
| Mixed | Both positive and negative | "Great features but too expensive." |
Using the Sentiment Analysis API
TinyFn provides an endpoint to analyze text sentiment:
POST https://api.tinyfn.io/v1/analyze/sentiment
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "I absolutely love this product! The quality is outstanding and the customer service was exceptional."
}
{
"sentiment": "positive",
"score": 0.92,
"scores": {
"positive": 0.92,
"negative": 0.02,
"neutral": 0.06
},
"emotions": {
"joy": 0.85,
"trust": 0.72,
"anticipation": 0.45
},
"confidence": 0.95
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | Text to analyze (required) |
include_emotions |
boolean | Include detailed emotion scores (default: true) |
language |
string | Text language hint (default: auto-detect) |
Code Examples
JavaScript / Node.js
const response = await fetch('https://api.tinyfn.io/v1/analyze/sentiment', {
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'The service was okay, but the food was terrible and overpriced.'
})
});
const data = await response.json();
console.log(`Sentiment: ${data.sentiment}`);
console.log(`Score: ${data.score}`);
// Output: Sentiment: negative, Score: -0.65
Python
import requests
def analyze_sentiment(text):
response = requests.post(
'https://api.tinyfn.io/v1/analyze/sentiment',
json={'text': text},
headers={'X-API-Key': 'your-api-key'}
)
return response.json()
# Analyze customer reviews
reviews = [
"Amazing product, exceeded expectations!",
"Disappointed with the quality.",
"It works as described."
]
for review in reviews:
result = analyze_sentiment(review)
print(f"{result['sentiment']}: {review[:30]}...")
cURL
curl -X POST "https://api.tinyfn.io/v1/analyze/sentiment" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text": "Best purchase I have ever made!"}'
Common Use Cases
- Customer Feedback: Analyze reviews and support tickets
- Social Media Monitoring: Track brand sentiment online
- Survey Analysis: Understand open-ended survey responses
- Content Moderation: Flag negative or hostile content
- Market Research: Gauge public opinion on products
Best Practices
- Clean the text: Remove irrelevant content (signatures, quotes)
- Handle sarcasm: Be aware that sarcasm can fool sentiment analyzers
- Consider context: Domain-specific terms may have different connotations
- Aggregate results: Analyze trends over many samples
Try the Sentiment Analysis API
Get your free API key and start analyzing sentiment in seconds.
Get Free API Key