Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Note that this is sandbox. If you have not registered profiles in sandbox you
will need to do so prior to making these calls. You will also need at least
one campaign, ad group, and keyword before you see anything in the report.
While in sandbox, we will return “dummy” data so that you can see how an actual
report would look.
To make it look nicer, I always export my access token prior to making calls.
You could do the same for API-Scope (profile Id) if you wish. Make sure to use
quotes around the access token if it isn’t URL encoded. It has a | (pipe)
symbol and your shell won’t like it.
$ export AUTH="MY_ACCESS_TOKEN"
1) Request the report:
$ curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: bearer $AUTH" \
-H "Amazon-Advertising-API-Scope: ABC1234567890" \
-d '{"reportDate": "20161010", "campaignType": "sponsoredProducts", "metrics": "impressions,clicks,cost", "segment": "query"}' \
https://advertising-api-test.amazon.com/v1/keywords/report
Response:
{
"reportId":"amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731",
"recordType":"keyword",
"status":"IN_PROGRESS",
"statusDetails":"Report is being generated."
}
2) Get the report (actually getting the location for downloading):
$ curl \
-H "Authorization: bearer $AUTH" \
-H "Amazon-Advertising-API-Scope: ABC1234567890" \
https://advertising-api-test.amazon.com/v1/reports/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731
Response:
{
"reportId":"amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731",
"status":"SUCCESS",
"statusDetails":"Report has been generated successfully. Pending publish to S3.",
"location":"https://advertising-api-test.amazon.com/v1/reports/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731/download",
"fileSize":236
}
3) Get the download location to S3. This is returned in the header. The file
is also gzipped. Notice the use of the -v flag so CURL will show headers.
$ curl -v \
-H "Authorization: bearer $AUTH" \
-H "Amazon-Advertising-API-Scope: ABC1234567890" \
https://advertising-api-test.amazon.com/v1/reports/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731/download
Response:
< HTTP/1.1 307 Temporary Redirect
< Date: Fri, 14 Oct 2016 21:17:49 GMT
< Server: Apache-Coyote/1.1
< Location: https://sandboxreports.s3.amazonaws.com/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731?AWSAccessKeyId=AKIAIKLHNT32USZOWVRA&Expires=1476479900&Signature=I%2F2Gd%2B8TbcPbXbBUM6ix%2BSVP3qA%3D
< x-amz-request-id: 067KMA80XJHXHQ1P37SA
< Content-Length: 0
< Vary: Accept-Encoding,User-Agent
4) “Location” is the S3 location where the report can be downloaded. You
don’t have to send authorization headers to get this. You can get it just
by visiting the URL. It will expire very quickly so be sure you get it ASAP.
You will probably need to put quotes around it as the URL contains characters
such as & that the shell will interpret as you trying to background the process,
which we don’t want.
$ curl -o /tmp/report.json.gz "https://sandboxreports.s3.amazonaws.com/amzn1.clicksAPI.v1.m1.580149D6.c7aa92c1-ca5b-435d-bb8b-51cb26ad5731?AWSAccessKeyId=AKIAIKLHNT32USZOWVRA&Expires=1476479900&Signature=I%2F2Gd%2B8TbcPbXbBUM6ix%2BSVP3qA%3D"
5) Profit:
$ less /tmp/report.json.gz
@garybunofsky

This comment has been minimized.

Copy link

garybunofsky commented Jun 13, 2018

Hey @dbrent-amazon. Thanks you for this gist. It's helped me quite a bit already. You seem to be pretty familiar with the Amazon Advertising API. I was hoping that you could help me, as I've searched high and low for clarification on my current issue but haven't had any luck.

My goal is to get current campaigns through the Amazon Advertising API so that I can generate reports. I've implemented LWA, and I'm having trouble getting profile data. I need to get profile data so I can pass the Amazon-Advertising-API-Scope in the header of subsequent requests.

When I make a GET request to https://advertising-api.amazon.com/v1/profiles I receive a response, which leads me to believe that my Authorization header is passed properly. However the response is an empty array. I would expect some type of profile to be in the array, as there are currently campaigns running under this account.

Do I have to create a profile for this account in order to view current campaigns? Is it possible to get the data of current campaigns with the Advertising API? Any help would be much appreciated

@Keita-N

This comment has been minimized.

Copy link

Keita-N commented Jan 16, 2019

I'm also dealing with the same trouble as @garybunofsky, Did you find the solution?

@dbrent-amazon

This comment has been minimized.

Copy link
Owner Author

dbrent-amazon commented May 1, 2019

@garybunofsky and @Keita-N, empty profile list is a completely separate issue from reporting. You should open a Jira Service Desk ticket for that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.