Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I authenticate and authorize my SEMrush API requests in Python


How do I authenticate and authorize my SEMrush API requests in Python


To authenticate and authorize SEMrush API requests in Python, you need to obtain an API key and include it with each API call. Here are the key steps:

1. Sign up for a SEMrush account and obtain an API key. The API key is a unique identifier used to authenticate API requests[1][2].

2. Install the python-semrush library, which provides a convenient wrapper around the SEMrush API[4]. You can install it from GitHub:

bash
pip install git+https://github.com/storerjeremy/python-semrush.git

3. Create a `SemrushClient` instance and pass your API key to the constructor[4]:

python
from python_semrush.semrush import SemrushClient

client = SemrushClient(key='your_semrush_api_key')

4. Use the `SemrushClient` to make API calls, such as retrieving domain rankings or launching a site audit[4]. For example:

python
result = client.domain_ranks(domain='example.com')

5. The SEMrush API enforces rate limits to prevent abuse. By default, you cannot make more than 10 requests per second from a single IP address[2]. The API documentation provides details on the rate limits for each endpoint.

6. If you need to make a large number of API requests, you can purchase additional API units as part of a paid SEMrush subscription[2]. The number of units required depends on the type of request and the amount of data returned.

By following these steps and including your API key with each request, you can authenticate and authorize your SEMrush API calls using Python. The python-semrush library simplifies the process of making API requests and handling the responses.

Citations:
[1] https://www.javatpoint.com/semrush-api-documentation
[2] https://www.semrush.com/kb/5-api
[3] https://hugoakhtari.com/en/automate-semrush-export-python/
[4] https://github.com/storerjeremy/python-semrush
[5] https://www.semrush.com/blog/beginners-guide-to-semrush-project-api/