Search code examples
rapi-keyhttrjsonlite

Trying to get data from an API


I'm trying to get some appointment data from a practice management software. I have an API key but I have no experience in the area.

I have tried to convert Curl code with little success. The api documentation is here https://github.com/redguava/cliniko-api

I am trying to convert this curl code

curl https://api.cliniko.com/v1/appointments \
  -u API_KEY: \
  -H 'Accept: application/json' \
  -H 'User-Agent: APP_VENDOR_NAME (APP_VENDOR_EMAIL)'

What I've tried: (yes this is from a curl to r converter)

require(httr)

headers = c(
  `Accept` = 'application/json',
  `User-Agent` = 'APP_VENDOR_NAME (APP_VENDOR_EMAIL)'
)

res <- httr::GET(url = 'https://api.cliniko.com/v1/appointments', 
httr::add_headers(.headers=headers), 
httr::authenticate('API_KEY', 'INSERTED MY API KEY'))

Any ideas would be greatly appreciated


Solution

  • httr::authenticate takes input username and password in the form httr::authenticate(username,password).

    Curl's authenticate takes argument username and password joined by by a :, i.e. username:password.

    In the example from the API documentation the curl command authenticates the username:password combination API_KEY:. Looking closely, we can see that after the : is blank. From this we can determine the username field should be 'API_KEY' and the password field should be ''.

    So you should change your curl command to:

    require(httr)
    
    headers = c(
      `Accept` = 'application/json',
      `User-Agent` = 'APP_VENDOR_NAME (APP_VENDOR_EMAIL)'
    )
    
    res <- httr::GET(url = 'https://api.cliniko.com/v1/appointments', 
    httr::add_headers(.headers=headers), 
    httr::authenticate('API_KEY', ''))
    

    Where API_KEY is your provided API key.