Text Similarity API
The Text Similarity API computes the similarity score between two pieces of text. It uses state-of-the-art NLP machine learning models to first embed (see our Embddings API) the texts into 768-dimension vectors, and then computes the cosine similarity between the two vectors.
From 6,123 users
Applications using this API
API Status
API Response Times
Average | P50 | P75 | P90 | P95 |
|---|---|---|---|---|
| 364ms | 398ms | 458ms | 683ms | 1203ms |
Similar APIs
Didn't find what you were looking for? Suggest an improvement
/v1/textsimilarity POST
https://api.api-ninjas.com/v1/textsimilarity
Returns a similarity score between 0 and 1 (1 is similar and 0 is dissimilar) of two given texts.
Body Parameters
text_1requiredFirst input text. Maximum 5000 characters.
text_2requiredSecond input text. Maximum 5000 characters.
Headers
X-Api-KeyrequiredAPI Key associated with your account.
Response
A JSON object with the following fields or an error if the request is unsuccessful.
similaritySimilarity score between 0 and 1, where 1 indicates identical texts and 0 indicates completely different texts.
Sample Request Live Demo!
Try our APIs in the API playground
Sign up for a free API key to get started — no credit card required.
https://api.api-ninjas.com/v1/textsimilarityHeaders
X-Api-KeyLog in or sign up to get your API KeySample Response
Code Examples
1
2
3
4
curl -X POST "https://api.api-ninjas.com/v1/textsimilarity" \
-H "X-Api-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"text_1": "This is an example sentence.", "text_2": "This is just another example sentence."}'If your programming language is not listed in the Code Example above, you can still make API calls by using a HTTP request library written in your programming language and following the above documentation.
Frequently Asked Questions
Can I use the Text Similarity API for commercial purposes?
Yes, but you must have a premium subscription. Commercial use of the Text Similarity API is not permitted on the free tier. See our pricing page to choose a plan that fits your needs.How do I get an API key and start using the Text Similarity API?
Sign up for a free account to instantly get your API key, then pass it in theX-Api-Keyheader on every request. The documentation above includes ready-to-run examples in cURL, Python, JavaScript, Node.js, and more.What happens if I exceed my quota for the month?
Your API requests will simply be denied once you hit your monthly quota — you will never be charged more than the plan you signed up for. To increase your quota, you can upgrade your plan any time on our pricing page.What happens if a request to the Text Similarity API fails?
The API responds with a standard HTTP status code and a JSON error message describing the problem. See our error codes reference for the full list of codes and how to resolve each one, or contact support if you need help.How does the Text Similarity API compute the similarity score?
The API embeds both inputs into 768-dimension vectors using state-of-the-art NLP models and then computes the cosine similarity between them, returning asimilarityscore between 0 (completely different) and 1 (identical). If you need the raw vectors instead of a single score, see the Embeddings API.What parameters do I send to the Text Similarity API?
Send a POST request with two required body parameters,text_1andtext_2, each up to a maximum of 5000 characters. You can try different inputs in the live demo on the API documentation page.What does the Text Similarity API response include?
The response is a JSON object containing a singlesimilarityfield, a number between 0 and 1 where 1 means the texts are identical and 0 means they are completely different. Unsuccessful requests instead return an error.What are some use cases for the Text Similarity API?
It is useful for semantic search, duplicate or plagiarism detection, clustering, and matching user queries to a knowledge base by comparing how closely two pieces of text relate. Pair it with the Embeddings API for vector storage or the Text Language API to detect the language of inputs first.