Using API Keys to Access TERN Data Services

Welcome to TERN Knowledge Base

Using API Keys to Access TERN Data Services

How to use your TERN API key

The TERN API key is used to authenticate requests to supported TERN APIs and data services.

This page explains how to configure and use your TERN API key with commonly used programming languages and tools, including R, Python, Swagger and command-line applications.

Authentication requirements may vary between TERN services. The examples below demonstrate common ways of supplying your API key. For complete examples of querying APIs, downloading data and accessing specific TERN data services, see the related guides linked from this page.

Create your API Key

Please refer to this page: Creating API Keys

Keep your API key secure

Treat your API key like a password. Do not share it or include it in public scripts, notebooks or repositories. Where supported, using a credentials file such as .netrc can help avoid embedding the key directly in code.

Using your API key

Your API key can be supplied in different ways depending on the application or programming language you are using.

The sections below show how to configure authentication using:

  • .netrc

  • R

  • Python

  • Swagger

  • curl

  • wget

  • FFmpeg

  • GDAL and related geospatial tools

  • other supported command-line applications

You only need to configure the method relevant to the tool or workflow you are using.

Store your API key using .netrc

If you regularly access TERN data services programmatically, you can store your API key in a .netrc file rather than including it directly in commands or scripts.

A .netrc file stores authentication credentials for specific hosts and can be used by supported tools and libraries, including curl, wget and Python requests.

Create a .netrc file

Create a file named .netrc in your home directory.

Add an entry for each TERN service you need to access:

machine ecoimages.tern.org.au login <your-api-key> password machine data.tern.org.au login <your-api-key> password

Replace <your-api-key> with your TERN API key.

Credentials in .netrc are associated with a specific hostname. If you access multiple TERN services, you may need to add a separate machine entry for each hostname.

Where to save the file

The location and filename depend on your operating system and the application you are using.

Environment

Typical location

Environment

Typical location

Linux

~/.netrc

macOS

~/.netrc

Windows

User home directory, typically C:\Users\<username>\; some applications use .netrc and others _netrc

WSL

~/.netrc inside the WSL/Linux home directory, e.g. /home/<username>/.netrc

Linux and macOS

Save the file as .netrc in your home directory:

~/.netrc

Restrict access so that only your user account can read or modify it:

chmod 600 ~/.netrc

Windows

Save the credentials file in your Windows user home directory, typically:

C:\Users\<username>\

Depending on the application, the expected filename may be .netrc or _netrc. Refer to the documentation for the application you are using.

Tip: When creating the file in a text editor such as Notepad, make sure it is not accidentally saved as .netrc.txt or _netrc.txt.

Windows Subsystem for Linux (WSL)

Applications running inside WSL use the Linux home directory rather than the Windows user home directory.

Save the file as:

/home/<username>/.netrc

and set the file permissions:

chmod 600 ~/.netrc

From Windows File Explorer, WSL files can also be accessed under:

\\wsl.localhost\<distribution>\home\<username>\

For example, for Ubuntu 24.04:

\\wsl.localhost\Ubuntu-24.04\home\<username>\

Once the credentials file is configured, supported applications can obtain your API key from it without requiring the key to be included directly in each command or script.

Using .netrc

Once .netrc is configured, supported applications can obtain your credentials from the file rather than requiring the API key to be included directly in your command or script.

For example, with curl:

curl --netrc "https://ecoimages.tern.org.au/api/whoami"

Or with Python requests:

import requests URI = "https://ecoimages.tern.org.au/api/whoami" response = requests.get(URI) response.raise_for_status() print(response.json())

In both examples, the API key is read from .netrc and does not need to be included directly in the command or script.

The sections below also show how to supply an API key directly when .netrc is not being used or when a particular workflow requires another authentication method.

Access a TERN API using R or Python

TERN APIs can be accessed programmatically using languages such as R and Python.

The following examples use the EcoImages /whoami endpoint as a simple way to verify that your TERN API key is working.

Replace <your-api-key> in the examples below with your TERN API key. Do not share your API key or include it in publicly accessible scripts or repositories.

R

The .netrc configuration described above is not automatically used by the httr2 example below. Supply your API key directly in the request using the X-Api-Key header.

The following example uses the httr2 package.

If httr2 is not already installed, install it with:

install.packages("httr2")

Then send a request to the EcoImages /whoami endpoint:

library(httr2) API_KEY <- "<your-api-key>" URI <- "https://ecoimages.tern.org.au/api/whoami" response <- request(URI) |> req_headers(`X-Api-Key` = API_KEY) |> req_perform() resp_body_json(response)

Python

The following example uses the requests package.

If requests is not already installed, install it with:

pip install requests

Then send a request to the EcoImages /whoami endpoint:

import requests API_KEY = "<your-api-key>" URI = "https://ecoimages.tern.org.au/api/whoami" headers = { "X-Api-Key": API_KEY } response = requests.get(URI, headers=headers) response.raise_for_status() print(response.json())

If authentication is successful, the API returns information associated with the authenticated user.

If .netrc is configured and available to your Python environment, requests can use the stored credentials automatically. In this case, you do not need to include the API key in your Python code:

import requests URI = "https://ecoimages.tern.org.au/api/whoami" response = requests.get(URI) response.raise_for_status() print(response.json())

These examples demonstrate the basic pattern for making an authenticated API request. The endpoint, parameters and returned data will vary depending on the TERN service and the information you want to access.

Explore and test an API using Swagger

Swagger provides an interactive web interface for exploring available API endpoints and testing requests without writing your own code.

TERN services that provide a Swagger interface are listed on the TERN REST APIs page.

The following example uses the EcoImages API Swagger interface to retrieve the list of packages available to the authenticated user.

Step 1 – Open the EcoImages Swagger interface

Open the EcoImages API Swagger interface in your web browser.

Click Authorize.

 

Step 2 – Enter your API key

Under ApiKeyAuth (apiKey):

a. Paste your TERN API key into the Value field.

b. Click Authorize.

c. Click Close.

 

Step 3 - Alternative authentication

If required, the BasicAuth (http, Basic) option can also be used. Enter your API key in the Username field, click Authorize, and then close the authentication window.

 

Step 4 – Select an API endpoint

Find and expand the GET /packages endpoint.

Click Try it out.

Click Execute

Step 5 – View the response

Swagger displays information about the request, including:

  • the request URL;

  • a generated curl command;

  • the HTTP response code; and

  • the response body.

A successful request returns an HTTP 200 response and a JSON response containing packages and pagination information.

 

The packages returned will depend on the data available to your account.

You can use the same general process to explore other endpoints in the EcoImages Swagger interface. For other TERN services, refer to the TERN REST APIs page and follow the documentation provided for that service.

 

Using your API key with curl

curl is a command-line tool that can be used to send requests to TERN APIs.

To authenticate a request, provide your TERN API key in the X-Api-Key HTTP header.

API key authentication

On Linux, macOS, Ubuntu/WSL, or Windows Command Prompt, run:

curl "https://ecoimages.tern.org.au/api/whoami" -H "X-Api-Key: <your-api-key>"

On Windows PowerShell, explicitly use curl.exe:

curl.exe "https://ecoimages.tern.org.au/api/whoami" -H "X-Api-Key: <your-api-key>"

Replace <your-api-key> with your TERN API key.

The EcoImages /whoami endpoint can be used to verify that your API key is working. If authentication is successful, the API returns information associated with the authenticated user.

Basic authentication

For TERN services that support HTTP Basic authentication, the API key can be supplied as the username with a blank password.

On Linux, macOS, Ubuntu/WSL, or Windows Command Prompt:

curl --user "<your-api-key>:" "https://ecoimages.tern.org.au/api/whoami"

On Windows PowerShell:

curl.exe --user "<your-api-key>:" "https://ecoimages.tern.org.au/api/whoami"

The colon (:) after <your-api-key> indicates a blank password.

If authentication is successful, the API returns the same user information as the API key authentication example above.

Windows note

Windows users can run these examples from either Command Prompt or PowerShell. In PowerShell, use curl.exe as shown above to ensure that the curl program is called directly.

Authentication methods may vary between TERN services. Refer to the documentation for the specific TERN service to determine which authentication method is supported.


Using your API key with wget

wget is a command-line tool that can be used to access and download data from supported TERN data services.

For services that support HTTP Basic authentication, supply your TERN API key as the username and leave the password blank.

On Linux, macOS, Ubuntu/WSL, or Windows Command Prompt:

wget --user="<your-api-key>" --password="" "https://data.tern.org.au/ecoacoustics/Calperum/Calperum01/spectrograms/2013/01/clpm-Calperum01_20130115_114444.png"

On Windows PowerShell, explicitly use wget.exe:

wget.exe --user="<your-api-key>" --password="" "https://data.tern.org.au/ecoacoustics/Calperum/Calperum01/spectrograms/2013/01/clpm-Calperum01_20130115_114444.png"

Replace <your-api-key> with your TERN API key.

The downloaded file is saved to your current working directory.

Windows note

wget may need to be installed separately on Windows. In PowerShell, use wget.exe to call the wget program directly.

For examples of downloading multiple files or collections of TERN data using wget, see [Downloading and accessing TERN data].


Using your API key with FFmpeg

FFmpeg can access remote audio and video files from supported TERN data services.

Unlike some command-line tools, FFmpeg does not automatically use the .netrc configuration described above when using its standard HTTP protocol. Authentication can instead be supplied through an HTTP Authorization header.

For an example of accessing and extracting part of a TERN acoustic recording with FFmpeg, see [Downloading and accessing TERN data].


GDAL and GDAL-based tools

GDAL and tools or libraries that use GDAL can authenticate to supported TERN data services using the .netrc configuration described above.

For example, the GDAL command-line tool gdalinfo can access a remote TERN raster using /vsicurl/:

gdalinfo /vsicurl/https://data.tern.org.au/gov/qld/fractional_cover_v3/landsat/fractional_cover/seasonal/qld/lztmre_qld_m202403202405_dp1a2.tif

This command can be run from Linux, macOS, Ubuntu/WSL, Windows Command Prompt, or Windows PowerShell, provided GDAL is installed and gdalinfo is available from the command line.

The /vsicurl/ prefix tells GDAL to access the raster remotely over HTTP rather than requiring you to download the complete file first.

If you do not use .netrc, credentials can instead be supplied through GDAL's HTTP authentication configuration.

For example, Rasterio uses GDAL for raster data access in Python:

import rasterio API_KEY = "<your-api-key>" url = "https://data.tern.org.au/gov/qld/fractional_cover_v3/landsat/fractional_cover/seasonal/qld/lztmre_qld_m202403202405_dp1a2.tif" with rasterio.Env( GDAL_HTTP_AUTH="BASIC", GDAL_HTTP_USERPWD=f"apikey:{API_KEY}" ): with rasterio.open(url) as src: print("Size:", src.width, "x", src.height) print("Bands:", src.count) print("CRS:", src.crs)

Replace <your-api-key> with your TERN API key.

For more examples of accessing remote TERN raster data using GDAL and GDAL-based tools, see [Downloading and accessing TERN data].

For more examples of remotely accessing and working with TERN raster data using GDAL and Rasterio, see Downloading and accessing TERN data.

 

 

 

Provide your feedback about the experience with Knowledge base