Getting started with API
Authorization
In order to use the API, it is necessary to obtain an API key from the SE Ranking user account. The API key must be transferred via the token parameter for each invoked method or via the Authorization HTTP header.
If an API method is invoked without an API key, or if an invalid API key is transferred in the request, the server returns a “no token” or “incorrect token” error.
Getting an API key
In order to start using the API, you must:
- Log in to the user account;
- Go to Settings -> API;
- Click the “Generate API Key” button.
Method Invocation
The API is accessible via the HTTPS protocol. All API method invocations are HTTP GET/POST/PUT/DELETE requests to the URL:
https://api4.seranking.com/
All received and output data is transmitted via the UTF-8 encoding standard. Data transmitted in the body of the POST/PUT request must be in JSON format.
If invocation is done successfully, an HTTP 2xx code is returned.
If an error occurs, an HTTP 4xx code or an HTTP 5xx code with a description of the error is returned.
curl -X GET “https://api4.seranking.com/account/balance” -H “Authorization: Token be2165b7d065e278e7305c1c7ef791f283f5d14b”
HTTP/1.0 200 OK
Content-Type: application/json
{“currency”:”rur”,”value”:2296380.85}
——
curl -X GET “https://api4.seranking.com/api/sites”
HTTP/1.0 403 Forbidden
Content-Type: application/json
{“message”:”No token”}
——
curl -X POST “https://api4.seranking.com” -H “Authorization: Token be2165b7d065e278e7305c1c7ef791f283f5d14b” -d “{\”url\”:\”http://example.com\”,\”title\”:\”example1\”}”
HTTP/1.0 201 Created
Content-Type: application/json
{“site_id”:147696}
$apiKey = “API_KEY”;
$url = “https://api4.seranking.com/sites”;
$context = stream_context_create([
“http” => [
“method” => “POST”,
“ignore_errors” => true,
“header” => [
“Authorization: Token $apiKey”,
“Content-Type: application/json; charset=utf-8”
],
“content” => json_encode([
“url” => “https://example.com”,
“title” => “my test project”
])
]
]);
$httpStatus = null;
$result = file_get_contents($url, 0, $context);
if (isset($http_response_header)) {
preg_match(“`HTTP/[0-9\.]+\s+([0-9]+)`”, $http_response_header[0], $matches);
$httpStatus = $matches[1];
}
if (!$result) {
echo “Request failed!”;
} else {
$result = json_decode($result);
if (201 == $httpStatus) {
echo $result->site_id;
} else {
echo “Error”.$result->message;
}
}
$url = “API_BASE_URL/sites”;
$token = “API_KEY”;
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => [“Authorization: Token “.$token],
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST =>true,
CURLOPT_POSTFIELDS=>json_encode([
“url” => “https://example.com”,
“titl” => “my new test project”
])
]);
$content = curl_exec($curl);
if (!$content) {
echo “Request failed!”;
} else {
$info = curl_getinfo($curl);
$result = json_decode($content);
if (201 == $info[“http_code”]) {
echo $result->site_id;
} else {
echo “Error”.$result->message;
}
}