Skip to content
PubIndexAPI

Code Examples

Code examples in various programming languages to make it easier for developers to integrate the Publisher Indexing API into their projects.

In all the examples we are using the /api/rss endpoint, but you can easily switch to another if you prefer.

Python

The following Python script sends a GET request to the Pub Index API every 30 seconds using the requests library, then parses the JSON response and prints the results each time - with a timestamp.

import requests
import time
from datetime import datetime

api_key = 'your_api_key'
feed_url = 'your_feed_url'
api_url = f'https://api.pubindex.dev/api/rss?feed={feed_url}&api-key={api_key}'

while True:
    response = requests.get(api_url)

    timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    if response.status_code == 200:
        data = response.json()
        print(f"{timestamp} - Success! Size: {data.get('size', 'N/A')}, Size Changed: {data.get('sizeChanged', 'N/A')}, WebSub Ping: {data.get('webSubPingSuccess', 'N/A')}, Google Ping: {data.get('googlePingSuccess', 'N/A')}")
    else:
        print(f"{timestamp} - Error: {response.status_code} - {response.text}")

    time.sleep(30)

Run the code on Replit

Replace your_api_key and your_feed_url with your actual API key and feed URL.

To use this script, make sure you have the requests library installed:

pip install requests

WordPress

Read the WordPress docs.

JavaScript/Serverless

Indexing App Starter

The Indexing App Starter is a Next.js app that you can deploy locally, or serverless to Vercel/your choice of host.

Read the Indexing App Starter docs.

PHP

Here’s a PHP example using the file_get_contents function:

<?php

$apiKey = 'your_api_key';
$feedUrl = 'your_feed_url';
$apiUrl = 'https://api.pubindex.dev/api/rss?feed=' . urlencode($feedUrl) . '&api-key=' . urlencode($apiKey);

try {
    $response = file_get_contents($apiUrl, false);

    if ($response === false) {
        throw new Exception('Failed to fetch data from the API.');
    }

    $data = json_decode($response, true);

    if (json_last_error() !== JSON_ERROR_NONE) {
        throw new Exception('Failed to decode JSON response.');
    }

    echo "Success! Size: {$data['size']}, Size Changed: {$data['sizeChanged']}, WebSub Ping: {$data['webSubPingSuccess']}, Google Ping: {$data['googlePingSuccess']}";

} catch (Exception $e) {
    echo "Error: {$e->getMessage()}";
}

Replace your_api_key and your_feed_url with your actual API key and feed URL.