API client that provide services of DataForSeo API For more information about client code generation, please follow to github
npm install dataforseo-clientbash
npm install dataforseo-client
`
Examples of usage
Example of live request
`typescript
import * as client from 'dataforseo-client'
async function main() {
const username = 'username';
const password = 'password';
const authFetch = createAuthenticatedFetch(username, password);
let serpApi = new client.SerpApi("https://api.dataforseo.com", { fetch: authFetch });
let task = new client.SerpGoogleOrganicLiveAdvancedRequestInfo();
task.location_code = 2840;
task.language_code = "en";
task.keyword = "albert einstein"
let resp = await serpApi.googleOrganicLiveAdvanced([task]);
}
function createAuthenticatedFetch(username: string, password: string) {
return (url: RequestInfo, init?: RequestInit): Promise => {
const token = btoa( ${username}:${password});
const authHeader = { 'Authorization': Basic ${token} };
const newInit: RequestInit = {
...init,
headers: {
...init?.headers,
...authHeader
}
};
return fetch(url, newInit);
};
}
main();
`
Example of Task based endpoint
`typescript
import * as client from 'dataforseo-client'
async function main() {
const username = 'username';
const password = 'password';
const authFetch = createAuthenticatedFetch(username, password);
let serpApi = new client.SerpApi("https://api.dataforseo.com", { fetch: authFetch });
let task = new client.SerpTaskRequestInfo();
task.location_code = 2840;
task.language_code = "en";
task.keyword = "albert einstein"
let taskResponse = await serpApi.googleOrganicTaskPost([task])
let taskID = taskResponse.tasks[0].id;
const startTime = Date.now();
while (!await isReady(serpApi, taskID) && Date.now() - startTime < 60000) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
let resp = await serpApi.googleOrganicTaskGetAdvanced(taskID);
}
async function isReady(serpApi: client.SerpApi, id: string): Promise {
let resp = await serpApi.googleOrganicTasksReady();
let isReadyId = false;
resp.tasks.forEach(x => {
if (x.id == id) {
isReadyId = true;
}
});
return isReadyId;
}
function createAuthenticatedFetch(username: string, password: string) {
return (url: RequestInfo, init?: RequestInit): Promise => {
const token = btoa( ${username}:${password});
const authHeader = { 'Authorization': Basic ${token} };
const newInit: RequestInit = {
...init,
headers: {
...init?.headers,
...authHeader
}
};
return fetch(url, newInit);
};
}
main();
``