Authorization
To use the Yandex Metrica API, you need to get an authorization token through the Yandex OAuth server. Pass the token for each method in the Authorization HTTP header.
GET /management/v1/counters HTTP/1.1
Host: api-metrika.yandex.net
Authorization: OAuth 05dd3dd84ff948fdae2bc4fb91f13e22bb1f289ceef0037
Content-Type: application/x-yametrika+json
Content-Length: 123
If an API method is called without a token or the request includes an invalid token, the server returns the HTTP status 401 Unauthorized.
Note
Authorization tokens must be stored securely and not given to third parties.
Obtaining the OAuth token
To get an authorization token:
-
Create an app and select For API access or debugging.
-
Fill in the information:
-
Name: Any name of your choice.
-
Email: Specify your preferred contact email.
-
Data access: Specify a set of access permissions for your app.
Access types:
- metrika:read: Getting statistics, reading parameters of custom and trusted tags, getting a list of tags.
- metrika:write: Creating tags, changing parameters of custom and trusted tags, uploading any data.
- metrika:expenses: Uploading expenses to tags.
- metrika:user_params: Uploading user parameters to tags.
- metrika:offline_data: Uploading offline data (CRM data, offline conversions, calls) to tags.
The metrika:expenses, metrika:user_params, and metrika:offline_data access permissions are optional if you use metrika:write.
Note
If you're using porg usernames (organization usernames), add passport:business to the access permissions. This is required to issue a token from the organization.
-
-
Click Create app and copy its ClientID (next to the ID, click ).
-
Add the copied ClientID to the link as follows
https://oauth.yandex.com/authorize?response_type=token&client_id=<application_id> -
Follow the link and copy your authorization token on the page that opens.
Troubleshooting
Error 403 (Access is denied) after obtaining a token
Possible reasons:
App-side
- The app doesn't have access to Yandex Metrica. To read tag data (for example, to generate reports or view tag information), your app requires
metrika:readaccess. To manage tags (for example, to upload offline data or edit tags and segments), your app requiresmetrika:writeaccess.
Token-side
-
The token is invalid. The token expired or the authorization password for the associated account was changed. Issue another token.
-
The token was issued for another account. It may have been issued for a username that doesn't have access to the Yandex Metrica tag.
Warning
The token owner is not the app owner but the account that you used to make the GET request to obtain the token.
-
The token was created for another app. The GET request to obtain the token included an incorrect
client_idvalue or a typo that resulted in the token being issued for an app that doesn't havemetrika:readormetrika:writeaccess to Yandex Metrica.
Yandex Metrica-side
- The token owner doesn't have access to the tag that you're attempting to access via the API. Learn more about the types of tag access. The Management API requires owner, guest view, or guest write access.
API request-side
- The token is read incorrectly or not at all due to incorrect authorization parameters in the API call code.
Error 401 (unauthorized) after obtaining a token
Possible reasons:
- The authorization parameters in the request header are incorrect.
- The header is missing authorization parameters.