DataForSEO API Client
The `dataforseo-client` is a comprehensive TypeScript API client designed to facilitate interaction with the DataForSEO API. It currently stands at version 2.0.23 and provides a streamlined interface to 12 distinct API sections, including AI Optimization, SERP, Keywords Data, Domain Analytics, and Backlinks. This client abstracts the complexities of raw HTTP requests and response parsing, offering developers ready-to-use methods for both live (synchronous HTTP request/response) and task-based (asynchronous, multi-step task submission and retrieval) API calls. Built from an OpenAPI specification, it ships with full TypeScript type definitions for an enhanced development experience. While a specific release cadence isn't published, its frequent updates and versioning suggest active development. A key advantage is its ability to handle DataForSEO's extensive data ecosystem through a consistent, type-safe programming interface, allowing users to focus on data utilization rather than integration specifics.
Common errors
-
Error: Request failed with status code 401
cause Invalid or missing API credentials (username/password) were provided during client initialization. This indicates an authentication failure.fixVerify that your `DATAFORSEO_USERNAME` (your DataForSEO account email) and `DATAFORSEO_PASSWORD` (the specific API password from your dashboard) are correct and correctly passed to the `DataForSeoClient` constructor. Ensure they have not been swapped. -
Error: Request failed with status code 400
cause One or more parameters in your API request body are invalid, missing, or the overall request body format is incorrect for the target endpoint.fixThoroughly review the DataForSEO API documentation for the specific endpoint you are calling to confirm all required parameters are present, correctly named, and adhere to the expected data types and formats (e.g., `language_code`, `location_code`, `keyword`). -
Error: Request failed with status code 429
cause You have exceeded the API's rate limit for the given endpoint within the specified time window.fixImplement a delay or a retry mechanism with exponential backoff between consecutive requests. If persistent, consider contacting DataForSEO support to discuss potential increases in your rate limits. -
TypeError: Cannot read properties of undefined (reading 'google')
cause This typically occurs when attempting to access a nested API section (e.g., `client.serp.google`) on an `undefined` or incorrectly initialized client object. This can happen if the `DataForSeoClient` constructor failed or the client object was not properly assigned.fixEnsure that `const client = new DataForSeoClient(username, password);` executes without errors and that the `client` variable holds a valid instance. Verify that the full access path to the API endpoint (e.g., `client.serp.google.organic.live.post`) accurately reflects the library's structure.
Warnings
- breaking DataForSEO's underlying API v2 has been deprecated and will cease to be supported as of May 5, 2026. This client library (`dataforseo-client` v2.x.x) is specifically designed to interact with DataForSEO API v3. Users migrating from older DataForSEO client libraries or direct API v2 calls must update their code to conform to API v3 specifications, which involves significant changes in endpoint paths, request bodies, and response structures.
- gotcha DataForSEO API uses Basic Authentication, requiring an API login and password. These are distinct from your general account login and password, and the API password is automatically generated. Providing incorrect credentials will result in authentication failures.
- gotcha DataForSEO APIs enforce rate limits per endpoint. Exceeding these limits can lead to HTTP 429 'Too Many Requests' responses, temporary blocking, or failed requests. The `X-RateLimit-Limit` and `X-RateLimit-Remaining` HTTP headers in responses indicate your current rate limit status.
- gotcha Many DataForSEO API operations, particularly for extensive data requests, utilize a 'task-based' asynchronous workflow. This involves posting a task, then polling a separate endpoint for the task's status, and finally retrieving the results once the task is complete. Directly expecting an immediate, complete response from an initial `POST` request for such endpoints will lead to incomplete data or errors.
Install
-
npm install dataforseo-client -
yarn add dataforseo-client -
pnpm add dataforseo-client
Imports
- DataForSeoClient
const DataForSeoClient = require('dataforseo-client').DataForSeoClientimport { DataForSeoClient } from 'dataforseo-client' - SerpGoogleOrganicLiveRequest
import { SerpGoogleOrganicLiveRequest } from 'dataforseo-client'import type { SerpGoogleOrganicLiveRequest } from 'dataforseo-client' - SerpGoogleOrganicLiveResponse
import type { SerpGoogleOrganicLiveResponse } from 'dataforseo-client'
Quickstart
import { DataForSeoClient } from 'dataforseo-client';
import type { SerpGoogleOrganicLiveRequest, SerpGoogleOrganicLiveResponse } from 'dataforseo-client';
// Ensure you set these environment variables or replace with actual credentials
const username = process.env.DATAFORSEO_USERNAME ?? 'YOUR_DATAFORSEO_USERNAME';
const password = process.env.DATAFORSEO_PASSWORD ?? 'YOUR_DATAFORSEO_PASSWORD';
async function runSerpLiveExample() {
if (username === 'YOUR_DATAFORSEO_USERNAME' || password === 'YOUR_DATAFORSEO_PASSWORD') {
console.error('ERROR: Please provide your DataForSEO API username and password. Find them at https://app.dataforseo.com/api-access.');
return;
}
const client = new DataForSeoClient(username, password);
try {
// Define the request payload for a live Google organic SERP search
const post_array: SerpGoogleOrganicLiveRequest[] = [
{
language_code: "en",
location_code: 2840, // Example: New York, United States
keyword: "best seo tools 2024",
se_domain: "google.com",
depth: 10, // Retrieve top 10 results
},
{
language_code: "en",
location_code: 2840, // Example: New York, United States
keyword: "content marketing strategies",
se_domain: "google.com",
depth: 5,
}
];
// Make the API call, explicitly typing the response
const response: SerpGoogleOrganicLiveResponse = await client.serp.google.organic.live.post(post_array);
if (response.status_code === 20000) {
console.log("API Call Successful. Processing results...");
response.tasks.forEach(task => {
if (task.result) {
task.result.forEach(result => {
result.items?.forEach(item => {
if (item.type === 'organic') {
console.log(`- Keyword: ${task.data?.keyword}`);
console.log(` Title: ${item.title}`);
console.log(` URL: ${item.url}`);
console.log(` Rank: ${item.rank_absolute}\n`);
}
});
});
}
});
} else {
console.error(`API Error: Status Code ${response.status_code} - ${response.status_message}`);
console.error("API Errors:", response.errors);
}
} catch (error) {
console.error("An unexpected error occurred during API call:", error);
if (error instanceof Error) {
console.error(error.message);
}
}
}
runSerpLiveExample();