Introduction

Use the Prefetch API to track actions like Page Views, Likes, Shares, and more across your site.

Steps

  1. One-time steps:

    • Register your domain by providing your email to get a Client ID and Client Secret.
    • Configure the API by verifying your domain and specifying valid pages and actions.
  2. Recurring steps:

    • Get a token (valid for 1 hour).
    • Use the Prefetch API to increment counters (e.g., pageviews, likes) or retrieve the current count.

You can also use our npm package for easy integration, which automatically updates and retrieves counts.


Register Domain

Request

POST
/api/v1/register
curl -X POST "https://prefetch.io/api/v1/register" \
  -H "Content-Type: application/json" \
  -d '{
        "clientName": "test-client-1",
        "domainName": "{{referer}}",
        "emailId": "{{emailId}}"
      }'

Response

{
  "clientId": "1b7ec7d8-9211-46f2-b14f-14313ac4e275",
  "clientSecret": "516698ca-8fef-4d93-afd8-2fc73aef6889"
}
Read more

Configure Valid Pages

Request

POST
/api/v1/configure
curl -X POST "https://prefetch.io/api/v1/configure" \
    -H "Content-Type: application/json" \
    -d '{
        "clientId": "{{clientId}}",
        "clientSecret": "{{clientSecret}}",
        "validPageIds": [
            "article-introduction-to-ssr-in-nextjs",
            "article-guide-to-dynamic-routing-in-nextjs",
            "article-best-practices-for-monorepo-setup",
            "article-how-to-deploy-nextjs-app-on-heroku",
            "article-understanding-react-server-components",
            "article-securing-your-nextjs-app-with-jwt",
            "article-using-shell-scripts-for-automation"
        ],
        "validActionIds": ['pageviews', 'favorites', 'likes', 'dislikes', 'shares'],
    }'

Response

{
  "clientId": "{{clientId}}",
  "clientSecret": "{{clientSecret}}",
  "validPageIds": [
    "article-introduction-to-ssr-in-nextjs",
    "article-guide-to-dynamic-routing-in-nextjs",
    "article-best-practices-for-monorepo-setup",
    "article-how-to-deploy-nextjs-app-on-heroku",
    "article-understanding-react-server-components",
    "article-securing-your-nextjs-app-with-jwt",
    "article-using-shell-scripts-for-automation"
  ],
  "validActionIds": ["pageviews", "favorites", "likes", "dislikes", "shares"]
}
Read more

Get Token

Request

GET
/api/v1/token
curl -X GET "https://prefetch.io/api/v1/token"

Response

{
  "token": "your-generated-token-string"
}
Read more

Increment count

Request

POST
/api/v1/pageviews/page-id/increment
curl -X POST "https://prefetch.io/api/v1/pageviews/page-id/increment" \
    -H "Content-Type: application/json" \
    -d '{"token": "{{token}}"}'

Response

{
  "count": 178
}
Read more

Getting count

Request

GET
/api/v1/pageviews/page-id/value
curl -X GET "https://prefetch.io/api/v1/pageviews/page-id/value" \
    -H "Authorization: {{token}}"

Response

{
  "count": 177
}
Read more

Guides

Register

Learn how to get clientId and clientSecret.

Read more

Configure

Learn how to programmatically configure pass list of pages on which the api will work.

Read more

Token

Read about the why we need to request a token and how to request it.

Read more

Increment

Understand how to increment pageviews counter and how to display the count.

Read more

Steps

Register

First step starts with registering a client to get a unique clientId and clientSecret for each domain.

Configure

Learn about the configure api which is used to restrict pages on which increment can be called.

Token

Learn about the token api which is used to ensure only valid page views are captured.

Increment

Learn about the increment api which is used to increment the pageviews counter using the token.

Was this page helpful?