DataForSEO API Client

2.0.23 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the DataForSEO client with credentials and perform a live SERP API request for Google organic results. It illustrates how to define request payloads using imported types, handle the API response, and iterate through the results, including basic error handling.

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();

view raw JSON →