Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I retrieve historical domain ranks with SEMrush API


How can I retrieve historical domain ranks with SEMrush API


here's how you can retrieve historical domain ranks using the SEMrush API:

1. Use the `domain_rank_history` endpoint: The SEMrush API provides a `domain_rank_history` endpoint that allows you to retrieve the historical ranking data for a given domain.

2. Specify the database: When making the API call, you need to specify the database you want to retrieve the data from. The available databases are listed in the SEMrush API documentation.

Here's an example of how to use the `domain_rank_history` endpoint in Python:

python
from python_semrush.semrush import SemrushClient

# Set your SEMrush API key
api_key = 'your_semrush_api_key'

# Create a SEMrushClient instance
client = SemrushClient(key=api_key)

# Get the historical domain rank for example.com in the US database
domain_rank_history = client.domain_rank_history(
    domain='example.com',
    database='us'
)

# Print the historical ranking data
for rank_data in domain_rank_history.ranks:
    print(f"Date: {rank_data.date}, Rank: {rank_data.rank}")

In this example, we're using the `domain_rank_history()` method of the `SemrushClient` class to retrieve the historical ranking data for the domain `'example.com'` in the `'us'` database.

The `domain_rank_history` object returned by the API call contains a `ranks` attribute, which is a list of `DomainRankHistoryRow` objects. Each of these objects represents the domain's rank on a specific date, and you can access the `date` and `rank` attributes to print the historical data.

You can customize the API call by specifying additional parameters, such as the `display_limit` to control the number of results returned, or the `display_offset` to retrieve data for different date ranges.

By using the SEMrush API's `domain_rank_history` endpoint, you can programmatically retrieve and analyze the historical ranking data for your own or your competitors' domains, which can provide valuable insights for your SEO and competitive analysis efforts.

Citations:
[1] https://github.com/silktide/semrush-api
[2] https://developer.semrush.com/api/
[3] https://developer.semrush.com/api/v3/analytics/domain-reports/
[4] https://developer.semrush.com/api/v3/projects/projects/
[5] https://developer.semrush.com/api/basics/get-started/