Chapter 1 API

1.1 Local Database Option

This makes sense for a single project, so specify which of your CTT projects will be loaded into the database with the “myproject” option set to the project name on your account. (To look up your project name by station, go to “My Stations” on your account and click on the station of interest to see which project it’s affiliated with.) If you choose to create a database out of your data (fair warning: in the future, the analysis tools will be based on this structure) you will need to install PostgreSQL on your machine.
Disclaimer I am providing some basic instructions for getting setup with Postgres on Windows and creating a user and database here, but please use at your own discretion and do not contact me with Postgres install questions. If any of these steps fail, seek another tutorial for installing Postgres and cross-reference with the steps here.

  1. [Download and install] ( for your OS
  2. If pgAdmin wasn’t installed with your PostgreSQL installation, it’s a nice GUI for interacting with and visualizing your database
  3. For simplicity, create a Postgres user with the same name as your computer user name. Otherwise, you will need to pass it as an argument to the connection
  4. Create a database in Postgres owned by that user name. You may have to set a password, and you may have to pass that password as an argument to the connection
  • you may need to update your pg_hba.conf file to use the “trust” method for your connections
  • if so, you’ll also need to reload the configuration/restart Postgres for the new settings to take

1.2 Your Token

Please request an API token through this form. The token will appear on your account page when the request is fulfilled.

1.3 R script: api_run.R

  1. As with the other R tools, I would suggest creating your own copy of “api_run.R” within your local repository, and modify that file.
  2. Assign the API token you found above to the “my_token” variable (line 6)
  3. Set your “outpath” variable to wherever your files will live. If you have already been manually downloading files, use that as your “outpath.”
  • The script will search that directory, and will only download files you haven’t already downloaded.
  • It will create a nested folder structure within that directory in the following order: project name, station(s), file types, files
  1. If you do not want to create a database…
  • comment out lines 8, 13-14
  • remove the “conn” argument from the get_my_data() function (line 11)
  1. If you do want to create a database locally, set “db_name” to the name of the Postgres database you created (line 7)

1.4 Terminal

Run “Rscript <path to your copy of api_run.R>” on the command line to run the script outside of RStudio (recommended)