NewConnect Claude, Gemini, ChatGPT, and other AI agents to API Ninjas via our MCP server

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.

(4.3)

From 6,123 users

500+

Applications using this API

API Status

Online - All Systems Operational

API Response Times

Average
P50
P75
P90
P95
364ms398ms458ms683ms1203ms

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_1  required

    First input text. Maximum 5000 characters.

  • text_2  required

    Second input text. Maximum 5000 characters.

Headers

  • X-Api-Key  required

    API Key associated with your account.

Response

A JSON object with the following fields or an error if the request is unsuccessful.

  • similarity

    Similarity score between 0 and 1, where 1 indicates identical texts and 0 indicates completely different texts.

Sample Request Live Demo!

text_1
text_2

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/textsimilarity

Headers

X-Api-KeyLog in or sign up to get your API Key

Sample Response

JSON
{
    similarity:0.8126154541969299
}

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

  • 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.
  • Sign up for a free account to instantly get your API key, then pass it in the X-Api-Key header on every request. The documentation above includes ready-to-run examples in cURL, Python, JavaScript, Node.js, and more.
  • 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.
  • 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.
  • 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 a similarity score between 0 (completely different) and 1 (identical). If you need the raw vectors instead of a single score, see the Embeddings API.
  • Send a POST request with two required body parameters, text_1 and text_2, each up to a maximum of 5000 characters. You can try different inputs in the live demo on the API documentation page.
  • The response is a JSON object containing a single similarity field, 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.
  • 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.