Skip to main content
Solved

How to call Amplitude Export API with R


Forum|alt.badge.img

I am trying to call the Amplitude Export API (link here: https://developers.amplitude.com/docs/export-api) with R.

Below is the explanation but I really don't see how to apply the following using a function like GET or curl functions.

curl -u API_Key:Secret_Key 'https://amplitude.com/api/2/export?start=20150201T5&end=2015020

Best answer by Saish Redkar

Hey Maxence,
Yup. The Export API outputs a zipped archive of JSON files, one or multiple files per hour depending on your input parameters.

I’m not well-versed with R that much but I use Python’s Request library pretty much for all my Amplitude API calls.

The httr webpage shows the following.

You can try extracting the output of the API call to a zipped file and then either manually unzipping it or extracting it using R code.

I did some digging and found this which might help you out. Hope this helps!

 

Is your use case to analyze the raw event json files in R?

View original
Did this topic help you find an answer to your question?

7 replies

Saish Redkar
Expert
Forum|alt.badge.img+10

Hey @Maxence 
Here are some online tools which I use to translate curl commands into a specific language 

You will have to use the httr R package to make it work. I believe this is similar to the Requests library in Python.

Let me know if this works!


Forum|alt.badge.img
  • Author
  • Novice
  • 3 replies
  • June 2, 2021

Many thanks @Saish Redkar  ! I managed to make the request as I get successful 200 http response.

BUT I don’t understand the data I retrieved. It’s some kind of gibberish as follow:

Do you know how I can decode this? 


Saish Redkar
Expert
Forum|alt.badge.img+10

Looks like it’s returning a raw vector.

As per the docs, httr will automatically decode content from the server using the encoding supplied in the content-type HTTP header. 

Try 

content(res, "text") 

or 

content(res, "text", encoding = "ISO-8859-1")

Forum|alt.badge.img
  • Author
  • Novice
  • 3 replies
  • June 3, 2021

This is what I get. I have a feeling the output could be a zip file as I see in the Export API but I don’t know how to read a zip file in R.

 

 


Saish Redkar
Expert
Forum|alt.badge.img+10
  • Expert
  • 1378 replies
  • Answer
  • June 3, 2021

Hey Maxence,
Yup. The Export API outputs a zipped archive of JSON files, one or multiple files per hour depending on your input parameters.

I’m not well-versed with R that much but I use Python’s Request library pretty much for all my Amplitude API calls.

The httr webpage shows the following.

You can try extracting the output of the API call to a zipped file and then either manually unzipping it or extracting it using R code.

I did some digging and found this which might help you out. Hope this helps!

 

Is your use case to analyze the raw event json files in R?


Forum|alt.badge.img
  • Author
  • Novice
  • 3 replies
  • June 3, 2021

@Saish Redkar you are a life savior ! :) 

From your suggestion I made it work by writing this:

bin <- content(res, "raw")
writeBin(bin, "myfile.zip")

Which downloaded mant zip files which contains many json files.

And I can read json files, thanks a lot ! :)


Saish Redkar
Expert
Forum|alt.badge.img+10

Glad that worked! Happy to have helped :)
 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings