This site uses cookies and other tracking technologies to make possible your usage of the website, assist with navigation and your ability to provide feedback, analyse your use of our products and services, assist with our promotional and marketing efforts, and provide better user experience.

By using the website, you agree to our Privacy policy

Accept and continue

Report a bug

Cancel
1425 5
News 9 min read May 15, 2019

Serpstat JavaScript SDK: Programming Right In Your Browser

Serpstat Javascript SDK: Programming Right In Your Browser

Andrey Belousov
Growth Hacking Specialist at Serpstat
Again and again, the question arises as to whether a good SEO must be able to code. So far, only one thing is clear — programming knowledge helps you increase the efficiency of your work and go beyond standard options of the SEO tools.
Recently, we published our first hacking article, which tells how to use the Serpstat script to analyze competitors. This article explains how to use Serpstat API to code directly in the browser to analyze large amounts of data for SEO purposes and to systemize them according to your needs.

Why do you need an API?

Although Serpstat has many features, your daily tasks may require special effort. If you need an unusual analysis of a huge amount of data for your SEO routine, then Serpstat API is a perfect solution for you.

High-quality keyword research, checking the top search results in seconds, quick competitive analysis and finding unusual solutions are just a few of the many advantages offered by Serpstat API. Everything from the analysis of the individual site to crawling with complex evaluations is at your fingertips.

Important! You don't need programming skills to work with API. You can find more information about the application of the interface in the manual.

Here are a few examples of the tasks that API can solve:
Analysis of positions and visibility of your site and your competitors' one
Assessment of SEO services' success by region
In-depth competitor analysis
Keyword research and URL analysis
Search for thematically related forums, blogs, and websites
 

New opportunities with JavaScript SDK

In addition to the API console, which makes the API application much easier, JavaScript SDK from Serpstat has recently become available. This interface allows you to code directly in the browser as well as on the Node.js platform.

In this case, no web server setup is required, thus making the API application faster and easier than ever. To get started, just open our code in the browser.

Start of the tool

1
Create an HTML file.
2
Open it with any text editor.
3
Add this code:
<script src="https://cdn.jsdelivr.net/npm/serpstat-api-3/serpstat-api.js"></script>
<pre id="res"></pre>
<script>
    var api = SerpstatAPI.init({
         token   : "ENTER TOKEN HERE" 
    });
   api.databases_info().then(function(bases){
      document.getElementById("res").innerHTML = JSON.stringify(bases,null,"  ");
   })
</script>
4
Enter your Serpstat API token instead of "ENTER TOKEN HERE."
5
Save the file.
6
Open it in your browser.
As you can see, you can easily repeat this. Let me just explain some details in the code:
Adding SDK:
<script src="https://cdn.jsdelivr.net/npm/serpstat-api-3/serpstat-api.js"></script>
The results are listed here:
<pre id="res"></pre>
<script>
Entering the token:
 var api = SerpstatAPI.init({
         token   : "ENTER TOKEN HERE" 
    });
Selecting API method:
api.databases_info()
After that, the code is executed below:
.then(function(bases){
Results are posted in the tag with id res:
document.getElementById("res").innerHTML = JSON.stringify(bases,null,"  ");

Async/Await or then?

Next, we'll use the keywords async/await in the code. Although they facilitate programming, they have one drawback: outdated browsers cannot support them. If you create a public utility, better use the keyword then:
async/await
async function someFun(){
    var res1 = await(api.someMethod1(...));
    …
    var res2 = await(api.someMethod2(...));
    ….
}
then
function someFun(){
    api.someMethod1(...).then(function(res1){
             ...
             api.someMethod2(...).then(function(res2){
                ...
              });
    });
}
This is how the code looks with async/await:
<script src="https://cdn.jsdelivr.net/npm/serpstat-api-3/serpstat-api.js"></script>
<pre id="res"></pre>
<script> (async function(){
    var api = SerpstatAPI.init({
         token   : "ENTER TOKEN HERE" 
    });
   var bases = await(api.databases_info());
   document.getElementById("res").innerHTML = JSON.stringify(bases,null,"  ");
})();</script>

Application of Serpstat API Console

Interface and examples

Another way to start working with API is to run the JS code in the API Console. To do this, choose the method Misc. and the option → Run Javascript Code.
For example, if you want to see all regions available for the analysis in Serpstat, you should:
1
Enter your token.
2
Select the method Run Javascript Code.
3
Enter the code:
var bases = await (api.databases_info());
return bases; 
I'll show you how to accomplish a more complicated task. Suppose you want to find information about your domain in all regions. This is the code that supplies the data to the Serpstat domain.
//get databases
var bases   = await (api.databases_info());
//leave an array of database codes
var se = [];
for(var i = 0; i<bases .length; i++){
    se.push(bases [i].db_name);      
}
//get domain data by region
var res = await(api.domain_info({
   query: 'serpstat.com',
   se: se   
}));
return res; 
The execution of the code will take several minutes. Then you get the report on the analyzed domain, where the rows show the domain parameters (visibility, traffic, etc.) and the columns show the regions.
If you want to display the data differently and switch columns and rows of the report, you can use the following code:

Important! Before you run the code, you should disable Remove Duplicates.
//get database
var bases   = await (api.databases_info());
//leave an array of database codes
var se = [];
for(var i = 0; i<bases.length; i++){
    if(bases [i].db_name[0]==='y'){
         se.push(bases [i].db_name);   
    }   
}
//get domain data by region
var res = await(api.domain_info({
   query: 'serpstat.com',
   se: se   
}));
//turn the table
var transope = [];
var rowNames = Object.keys(res[0]) 
for(var i=0; i<rowNames.length; i++){
    var rowName = rowNames[i];
    var row     = {name: rowName};
    for(var j=0;j<res.length;j++){
        row['se_'+res[j]._se] = res[j][rowName];
    }
    transope.push(row);
}
return transope; 
The API Console has its own interface, which offers a variety of functions for displaying the code and editing the data: tables, filters, SQL and export in CSV and XLSX formats. In other words, you can play your own code, export received data as XLSX or CSV file and open it with MS Excel or Google Spreadsheets.

Options application

When "Show options" is active under the code entry field, then you can use the options that are entered into the console in the code. For example, opts.query or opts.se.
var res = await(api.domain_info({
   query: opts.query,
   se: opts.se  
}));
return res; 
Or:
var res = await(api.domain_info(opts));
return res; 
As you can see, Serpstat API opens new horizons for SEOs. Especially when it comes to large agencies and in-house teams, for whom the automation of SEO tasks is a must-have. It's easier than ever to code with Serpstat API Console because it doesn't require server setup and takes no more than 10 minutes to initialize.

Learn how to get the most out of Serpstat

Want to get a personal demo, trial period or bunch of successful use cases?

Send a request and our expert will contact you ;)

Rate the article on a five-point scale

The article has already been rated by 3 people on average 3.67 out of 5
Found an error? Select it and press Ctrl + Enter to tell us

Share this article with your friends

Sign In Free Sign Up

You’ve reached your query limit.

Or email
Forgot password?
Or email
Back To Login

Don’t worry! Just fill in your email and we’ll send over your password.

Are you sure?

Awesome!

To complete your registration you need to enter your phone number

Back

We sent confirmation code to your phone number

Your phone Resend code Queries left

Something went wrong.

Contact our support team
Or confirm the registration using the Telegram bot Follow this link
Please pick the project to work on

Personal demonstration

Serpstat is all about saving time, and we want to save yours! One of our specialists will contact you and discuss options going forward.

These may include a personal demonstration, a trial period, comprehensive training articles & webinar recordings, and custom advice from a Serpstat specialist. It is our goal to make you feel comfortable while using Serpstat.

Name

Email

Phone

We are glad of your comment
Upgrade your plan

Upgrade your plan

Export is not available for your account. Please upgrade to Lite or higher to get access to the tool. Learn more

Sign Up Free

Спасибо, мы с вами свяжемся в ближайшее время

Invite
View Editing

E-mail
Message
Optional
E-mail
Message
Optional

You have run out of limits

You have reached the limit for the number of created projects. You cannot create new projects unless you increase the limits or delete existing projects.

I want more limits