TypeScript client library for Diffbot APIs. This is a port of the diffbot-python public library API.
pnpm add diffbot-typescriptRequires Node.js 18+ (native fetch).
In TypeScript, all API functions are async. Use await or for await where the Python SDK used sync iteration.
Create a client once, then pass it to the API functions you need. Import only the functions you use so bundlers can tree-shake unused code.
import{DiffbotClient,extract,resolveToken}from"diffbot-typescript";constdb=newDiffbotClient({token: resolveToken()});constdata=awaitextract(db,"https://www.example.com");awaitdb.close();Token resolution order:
- Explicit argument to
resolveToken(token) DIFFBOT_API_TOKENenvironment variableDIFFBOT_API_TOKEN=...line in~/.diffbot/credentials
import{DiffbotClient,extract}from"diffbot-typescript";constdb=newDiffbotClient({token: "YOUR_TOKEN"});constdata=awaitextract(db,"https://www.example.com");import{ask}from"diffbot-typescript";forawait(constchunkofask(db,[{role: "user",content: "What's the capital of France?"}])){process.stdout.write(chunk);}import{crawl}from"diffbot-typescript";forawait(consteventofcrawl(db,"https://www.example.com",{hops: 1})){console.log(event);}import{dql}from"diffbot-typescript";constresults=awaitdql(db,'type:Organization name:"Diffbot"');import{webSearch}from"diffbot-typescript";constresults=awaitwebSearch(db,"diffbot knowledge graph");for(constrofresults.search_resultsasArray<Record<string,unknown>>){console.log(r.score,r.title,r.pageUrl);}import{entities}from"diffbot-typescript";constresult=awaitentities(db,"Apple CEO Tim Cook announced record quarterly earnings.");DiffbotClient supports explicit resource management for automatic cleanup:
import{DiffbotClient,extract}from"diffbot-typescript";await using db=newDiffbotClient({token: "YOUR_TOKEN"});constdata=awaitextract(db,"https://www.example.com");| Python | TypeScript |
|---|---|
resolve_token() | resolveToken() |
crawl_list_jobs() | crawlListJobs() |
crawl_get_job() | crawlGetJob() |
crawl_delete_job() | crawlDeleteJob() |
dql_parallel() | dqlParallel() |
dql_fetch_ontology() | dqlFetchOntology() |
dql_refresh_ontology() | dqlRefreshOntology() |
web_search() | webSearch() |
pnpm install
pnpm test
pnpm buildLive integration tests (requires DIFFBOT_API_TOKEN):
pnpm test:liveMIT