Skip to content

Create or Update a Custom API

POSThttps://api.diffbot.com/v3/custom

Create or update the parameters and ruleset of an existing Custom API.

A Custom API starts as either an extension of an existing Extract API (allowing you to override or correct data returned from it), or as a blank canvas API with which rules can be given.

Custom APIs can be created and updated on the dashboard or via API. For a detailed walkthrough, follow the Getting Started with Custom API guide. This doc will focus on some quickstart examples, best practices, and the API reference.

Custom APIs use Diffbot's cloud-based rendering engine, and fully execute most page-level Javascript in order to access Ajax-delivered elements.

The ruleset is sent as a JSON request body. See Custom API Rulesets for the full ruleset format.

Run In Postman

Quickstart — Creating a Custom API

This example will create a Custom API that extends the Article API to extract author out of a blog post page on diffbot.com.

python
import requests

url = "https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>"

payload = {
    "rules": [
        {
            "name": "author",
            "selector": ".mb-1.text-dark strong"
        }
    ],
    "api": "/api/article",
    "urlPattern": "(http(s)?://)?(.*\\.)?www.diffbot.com.*",
    "testUrl": "https://www.diffbot.com/insights/build-a-sanctions-tracker/"
}

headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json'
}

response = requests.request("POST", url, headers=headers, json=payload)

print(response.text)
javascript
const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Accept", "application/json");

const payload = {
  "rules": [
      {
          "name": "author",
          "selector": ".mb-1.text-dark strong"
      }
  ],
  "api": "/api/article",
  "urlPattern": "(http(s)?://)?(.*\\.)?www.diffbot.com.*",
  "testUrl": "https://www.diffbot.com/insights/build-a-sanctions-tracker/"
}

const requestOptions = {
  method: "POST",
  headers: headers,
  body: JSON.stringify(payload),
  redirect: "follow"
};

fetch("https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
bash
curl --location --globoff 'https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
  "rules": [
    {
      "name": "author",
      "selector": ".mb-1.text-dark strong"
    }
  ],
  "api": "/api/article",
  "urlPattern": "(http(s)?://)?(.*\\.)?www.diffbot.com.*",
  "testUrl": "https://www.diffbot.com/insights/build-a-sanctions-tracker/"
}'

On success, you should receive a JSON response with a hashes attribute. The value of this attribute can be safely ignored or stored for identification. It is not used by any public Diffbot APIs.

json
{ "hashes": ["1234567"] }

Quickstart — Updating a Custom API

To update an existing Custom API, the values of urlPattern and api in the JSON payload have to exactly match those of an existing Custom API. If either of these values are changed, a new Custom API is created. Furthermore, the rules object will be replaced completely.

The following example will edit the quickstart Custom API created above and update the rules object to extract the subheader of the same page.

python
import requests

url = "https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>"

payload = {
    "rules": [
        {
            "name": "subheader",
            "selector": "#slice-hero p.lead"
        }
    ],
    "api": "/api/article",
    "urlPattern": "(http(s)?://)?(.*\\.)?www.diffbot.com.*",
    "testUrl": "https://www.diffbot.com/insights/build-a-sanctions-tracker/"
}

headers = {
  'Content-Type': 'application/json',
  'Accept': 'application/json'
}

response = requests.request("POST", url, headers=headers, json=payload)

print(response.text)
javascript
const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Accept", "application/json");

const payload = {
  "rules": [
      {
          "name": "subheader",
          "selector": "#slice-hero p.lead"
      }
  ],
  "api": "/api/article",
  "urlPattern": "(http(s)?://)?(.*\\.)?www.diffbot.com.*",
  "testUrl": "https://www.diffbot.com/insights/build-a-sanctions-tracker/"
}

const requestOptions = {
  method: "POST",
  headers: headers,
  body: JSON.stringify(payload),
  redirect: "follow"
};

fetch("https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
bash
curl --location --globoff 'https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
  "rules": [
    {
      "name": "subheader",
      "selector": "#slice-hero p.lead"
    }
  ],
  "api": "/api/article",
  "urlPattern": "(http(s)?://)?(.*\\.)?www.diffbot.com.*",
  "testUrl": "https://www.diffbot.com/insights/build-a-sanctions-tracker/"
}'

On success, you should receive a JSON response with a hashes attribute. As further confirmation that a Custom API was updated (and not created), the value of the hashes attribute will exactly match the hashes value returned when the Custom API was created earlier.

Note that the final updated Custom API will not extract an author attribute anymore because the rule for it was not included in the new rules object.

API Definition

Generated from v1.1.0 of the Extract APIs OpenAPI specification.

Create or update the parameters and ruleset of an existing Custom API

Request

Authentication
tokenquery
Cache and insert token into example requests (15d expiry)
Request body application/json
notesstring[]

An array of strings that can be added manually. The API automatically adds a notes specifying when the API was last updated.

xForwardHeadersobject

Allows you to pass in X-Forward headers by name, including X-Evaluate (aka. X-Forward-X-Evaluate; omit “X-Forward” in the header name)

X-Evaluatestring
rulesobject[]

An object that defines a set of rules for a specific urlPattern-api combination

namestring
selectorstring
filtersobject[]
argsstring[]
typestring
apistring

The specific API being targeted. Always precede the API name with “/api/” as in "/api/article" except for “all”)

urlPatternstring

A regex that defines the URLs for which the ruleset will be applied

testUrlstring

A URL that can be used to check that the rule still works as intended. This is the page that will load automatically when editing the ruleset in the Dashboard UI

renderOptionsstring

Rendering options

prefiltersstring[]

An array of string selectors that should be omitted from the DOM before extraction occurs

useProxystring

Used to disable proxies (when they have been set globally), by applying the value "none"

Example request

bash
curl --request POST \
  --url 'https://api.diffbot.com/v3/custom?token=<DIFFBOT_TOKEN>' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data-raw '{}'

Response

200Successful API Response
hashesstring[]
json
{
  "hashes": [
    "abcd1234"
  ]
}