Skip to main content

Count Occurrences API

Written by Kadri - RichAPI Support Team

POST /count_occurrences • Credits: 0.5 credits per call

Count Occurrences splits a string on a separator and counts how many times each item appears. The response holds total_items, unique_items and a results array that lists each item with its count, sorted by count. Each call costs 0.5 credits.

What does Count Occurrences do?

A workflow often holds one long string of values: tags from a scraped page, skills from a profile, one column from a CSV export. Count Occurrences turns that string into a frequency table. The endpoint splits the string on the separator that you send. It trims the whitespace around each item and drops every empty item. It then counts the items and returns them sorted by count, from the most frequent to the least frequent. Count Occurrences runs inside RichAPI and calls no external service.

Which endpoint does Count Occurrences use?

  • Method: POST

  • URL: https://api.richapi.ai/api/v1/count_occurrences

  • Authentication: x-api-key header

  • Response: Synchronous JSON

  • Credits: 0.5 credits per call

How to send a Count Occurrences request

  1. Copy your API key from the RichAPI dashboard.

  2. Put the values into one string in the input parameter.

  3. Set the separator parameter to the delimiter that divides the values.

  4. Send a POST request to https://api.richapi.ai/api/v1/count_occurrences.

  5. Send the API key in the x-api-key header.

  6. Read the results array from the response.

CAUTION: Each Count Occurrences call costs 0.5 credits. A call that returns an empty results array also costs 0.5 credits.

cURL

curl -X POST https://api.richapi.ai/api/v1/count_occurrences   
-H "x-api-key: YOUR_API_KEY"
-H "Content-Type: application/json"
-d '{
"input": "SaaS, AI, saas, security, AI, ai, SaaS , , devops",
"separator": ",",
"case_insensitive": true,
"limit": 0
}'

Python

import requests

response = requests.post(
"https://api.richapi.ai/api/v1/count_occurrences",
headers={"x-api-key": "YOUR_API_KEY"},
json={
"input": "SaaS, AI, saas, security, AI, ai, SaaS , , devops",
"separator": ",",
"case_insensitive": True,
"limit": 0,
},
timeout=60,
)
response.raise_for_status()
print(response.json())

Node.js

const response = await fetch("https://api.richapi.ai/api/v1/count_occurrences", {
method: "POST",
headers: {
"x-api-key": "YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
input: "SaaS, AI, saas, security, AI, ai, SaaS , , devops",
separator: ",",
case_insensitive: true,
limit: 0,
}),
});

const data = await response.json();
console.log(data);

Which parameters does Count Occurrences accept?

Parameter

Type

Required

Default

Description

input

string

Yes

None

The string to split and count. The maximum length is 1,048,576 characters.

separator

string

Yes

None

The delimiter that splits input into items. A comma, a space and a pipe are all valid values.

case_insensitive

boolean

No

true

Set to false to count SaaS and saas as two separate items.

limit

integer

No

0

The maximum number of objects in results. 0 returns every item. The maximum is 1000.

Count Occurrences accepts these four parameters and no other field. If the request body holds an unknown field, the API returns status 400 with the message Unknown input field(s): <name>.

The API reference shows a default of , for separator. That default is unreachable. If the request body omits separator, the API returns status 400 with the message Missing required field: separator.

What does Count Occurrences return?

Count Occurrences returns one flat JSON object. Here is the example response for the request above, which sends the string SaaS, AI, saas, security, AI, ai, SaaS , , devops with the separator ,.

{
"total_items": 8,
"unique_items": 4,
"results": [
{ "item": "ai", "count": 3 },
{ "item": "saas", "count": 3 },
{ "item": "devops", "count": 1 },
{ "item": "security", "count": 1 }
]
}

Which fields does the Count Occurrences response hold?

Field

Type

Description

total_items

integer

The number of items that remain after the split, the whitespace trim and the removal of the empty items.

unique_items

integer

The number of distinct items in input.

results

array of objects

One object per distinct item, sorted by count from high to low.

results[].item

string

The item. If case_insensitive is true, the value is lowercase.

results[].count

integer

The number of times the item appears in input.

The limit parameter truncates results only. Count Occurrences computes total_items and unique_items across the whole input, before the truncation.

How does Count Occurrences count the items?

Count Occurrences applies these rules in order:

  1. Count Occurrences splits input on the exact separator string.

  2. Count Occurrences trims the whitespace at both ends of each item.

  3. Count Occurrences drops every item that is empty after the trim.

  4. If case_insensitive is true, Count Occurrences converts each item to lowercase.

  5. Count Occurrences counts the items and sorts them by count, from high to low.

  6. If two items hold the same count, Count Occurrences sorts those two in alphabetical order.

  7. If limit is greater than 0, Count Occurrences keeps the first limit objects.

This table walks the example input SaaS, AI, saas, security, AI, ai, SaaS , , devops through those rules. The separator is , and case_insensitive is true.

#

Raw segment

After the trim

Counted as

In the count?

1

SaaS

SaaS

saas

Yes

2

AI

AI

ai

Yes

3

saas

saas

saas

Yes

4

security

security

security

Yes

5

AI

AI

ai

Yes

6

ai

ai

ai

Yes

7

SaaS

SaaS

saas

Yes

8

empty

none

No. The segment is empty after the trim.

9

devops

devops

devops

Yes

Nine segments enter, and eight items are counted. The eighth segment holds one space, so Count Occurrences drops it. total_items is therefore 8, not 9.

Four distinct items remain, so unique_items is 4. The counts are saas 3, ai 3, security 1 and devops 1. Rule 6 breaks the two ties: ai comes before saas, and devops comes before security.

Is Count Occurrences case sensitive?

Count Occurrences is case insensitive by default. The case_insensitive parameter defaults to true, so SaaS, saas and SAAS collapse into one item. The item value in the response is then lowercase. In the example above, the three differently cased SaaS segments produce one result object with a count of 3. To count each spelling on its own, set case_insensitive to false. The item values then keep their original case.

Does Count Occurrences count overlapping matches?

Count Occurrences counts no overlapping match, because separator is a delimiter and not a search term. The endpoint splits the string at every separator and counts what remains between the separators. The separator itself is never counted.

Two examples make the rule concrete.

Request body

Response

Why

{"input": "hello world hello", "separator": "hello"}

{"total_items": 1, "unique_items": 1, "results": [{"item": "world", "count": 1}]}

The split removes both instances of hello and leaves world.

{"input": "aaaa", "separator": "aa"}

{"total_items": 0, "unique_items": 0, "results": []}

The split leaves three empty segments, and rule 3 drops all three.

To count how often one term appears in a block of text, split the text into words. Set separator to one space, then read the count of that term from results.

How do you score a lead with Count Occurrences?

Keyword scoring is the common use of Count Occurrences. You take the text of a lead, count the words, and read the count of the keywords that matter to you. Follow these steps:

  1. Send a Web Scraper request for the website of the lead.

  2. Read data.markdown from the Web Scraper response.

  3. Send that text as input to Count Occurrences, with separator set to one space.

  4. Set limit to the number of top words that you want to keep.

  5. Read the count of each keyword from the results array.

This request scores one profile summary of a lead:

curl -X POST https://api.richapi.ai/api/v1/count_occurrences   
-H "x-api-key: YOUR_API_KEY"
-H "Content-Type: application/json"
-d '{
"input": "We build AI security tooling for SaaS teams. Our AI platform ships SaaS security controls.",
"separator": " ",
"case_insensitive": true,
"limit": 5
}'

The response holds the five most frequent words:

{
"total_items": 15,
"unique_items": 12,
"results": [
{ "item": "ai", "count": 2 },
{ "item": "saas", "count": 2 },
{ "item": "security", "count": 2 },
{ "item": "build", "count": 1 },
{ "item": "controls.", "count": 1 }
]
}

The summary scores ai 2, saas 2 and security 2. A lead score of 6 follows from the sum of those three counts.

Two details change a word count. Count Occurrences splits on the separator alone, so punctuation stays attached to the word. The last word of the summary is therefore controls. with a full stop. A double space between two words creates an empty segment, and Count Occurrences drops that segment before the count.

How much does Count Occurrences cost?

Count Occurrences costs 0.5 credits per call. At $0.02 a credit, the rate on the 1,000 and 5,000 credit packs, one call costs $0.01. Larger packs cost less per credit. The credit count does not change with the length of the input or with the number of items in the response.

Calls

Credits

Cost

1

0.5

$0.01

100

50

$1.00

1,000

500

$10.00

10,000

5,000

$100.00

The keyword scoring flow above costs 1.5 credits per lead, or $0.03. One Web Scraper call costs 1 credit, and one Count Occurrences call costs 0.5 credits.

Which errors can Count Occurrences return?

Status

Meaning

What to do

400

The request body failed validation, or input is empty.

Correct the field that the message names. Send both input and separator.

401

The API key is missing, wrong or revoked.

Send a valid key in the x-api-key header.

402

The organization has too few credits left, or the team reached its monthly spend limit.

Buy credits in the RichAPI dashboard.

403

This API key has no access to this endpoint (Endpoint not available on your plan).

Call GET /api/v1/my-endpoints to see the endpoints that this key can use. Contact support for access.

404

The endpoint name is wrong.

Send the request to /count_occurrences.

405

The request used a method other than POST.

Send the request with the POST method.

429

Too many requests.

Wait, then send the request again.

500

The API failed.

Send the request again. If the failure repeats, contact support.

RichAPI charges no credit for a 400, 401, 402, 403, 404, 405, 429 or 500 response. A 400 response names the field and lists the fields that Count Occurrences accepts:

{
"error": "Missing required field: separator",
"supported_fields": {
"accepted": ["case_insensitive", "input", "limit", "separator"],
"required": ["input", "separator"]
}
}

Frequently asked questions

Does Count Occurrences count how many times a substring appears?

No. Count Occurrences treats separator as a delimiter, not as a search term, and it never returns a count for the separator itself. To count one term inside a block of text, set separator to one space and read the count of that term from results.

Does Count Occurrences accept several search terms in one call?

Count Occurrences takes no search term at all. One call counts every distinct item in the input at once, so one response already holds the count of each term that you care about.

Can I send input as a JSON array?

No. The input parameter takes a string. If you send an array or a number, Count Occurrences returns status 400 with the message Invalid value for input: expected string.

How do I return only the top 10 items?

Set limit to 10. The results array then holds the 10 items with the highest counts, and total_items and unique_items still describe the whole input.

Does a call that finds nothing cost a credit?

Yes. Count Occurrences costs 0.5 credits for every 200 response, and an empty results array is a 200 response. A 4xx or 5xx error costs no credit.

Why does case_insensitive: "false" still count case insensitively?

Count Occurrences reads case_insensitive as a JSON boolean. A quoted string such as "false" is a non-empty value, and Count Occurrences treats it as true. Send false without the quotation marks.

Can I run Count Occurrences from n8n or from an MCP client?

Yes. The n8n operation name is count_occurrences. The MCP tool name is count_occurrences. Both take the same four parameters as the HTTP endpoint.

Related articles

Did this answer your question?