AG真人百家乐官方网站

Skip to main content
NSF NEON, Operated by Battelle

Main navigation

  • AG真人百家乐官方网站 Us
    • Overview
      • Spatial and Temporal Design
      • History
    • Vision and Management
    • Advisory Groups
      • Science, Technology & Education Advisory Committee
      • Technical Working Groups (TWGs)
    • FAQ
    • Contact Us
      • Contact NEON Biorepository
      • Field Offices
    • User Accounts
    • Staff
    • Code of Conduct

    AG真人百家乐官方网站 Us

  • Data & Samples
    • Data Portal
      • Spatial Data & Maps
    • Data Themes
      • Biogeochemistry
      • Ecohydrology
      • Land Cover and Processes
      • Organisms, Populations, and Communities
    • Samples & Specimens
      • Discover and Use NEON Samples
        • Sample Types
        • Sample Repositories
        • Megapit and Distributed Initial Characterization Soil Archives
      • Sample Processing
      • Sample Quality
    • Collection Methods
      • Protocols & Standardized Methods
      • Airborne Remote Sensing
        • Flight Box Design
        • Flight Schedules and Coverage
        • Daily Flight Reports
          • AOP Flight Report Sign Up
        • Camera
        • Imaging Spectrometer
        • Lidar
      • Automated Instruments
        • Site Level Sampling Design
        • Sensor Collection Frequency
        • Instrumented Collection Types
          • Meteorology
          • Phenocams
          • Soil Sensors
          • Ground Water
          • Surface Water
      • Observational Sampling
        • Site Level Sampling Design
        • Sampling Schedules
        • Observation Types
          • Aquatic Organisms
            • Aquatic Microbes
            • Fish
            • Macroinvertebrates & Zooplankton
            • Periphyton, Phytoplankton, and Aquatic Plants
          • Terrestrial Organisms
            • Birds
            • Ground Beetles
            • Mosquitoes
            • Small Mammals
            • Soil Microbes
            • Terrestrial Plants
            • Ticks
          • Hydrology & Geomorphology
            • Discharge
            • Geomorphology
          • Biogeochemistry
          • DNA Sequences
          • Pathogens
          • Sediments
          • Soils
            • Soil Descriptions
        • Optimizing the Observational Sampling Designs
    • Data Notifications
    • Data Guidelines and Policies
      • Acknowledging and Citing NEON
      • Publishing Research Outputs
      • Usage Policies
    • Data Management
      • Data Availability
      • Data Formats and Conventions
      • Data Processing
      • Data Quality
      • Data Product Bundles
      • Data Product Revisions and Releases
        • Release 2021
        • Release 2022
        • Release 2023
        • Release 2024
        • Release-2025
      • NEON and Google
      • Externally Hosted Data

    Data & Samples

  • Field Sites
    • AG真人百家乐官方网站 Field Sites and Domains
    • Explore Field Sites

    Field Sites

  • Impact
    • Observatory Blog
    • Case Studies
    • Papers & Publications
    • Newsroom
      • NEON in the News
      • Newsletter Archive
      • Newsletter Sign Up

    Impact

  • Resources
    • Getting Started with NEON Data & Resources
    • Documents and Communication Resources
      • Papers & Publications
      • Outreach Materials
    • Code Hub
      • Code Resources Guidelines
      • Code Resources Submission
    • Learning Hub
      • Science Videos
      • Tutorials
      • Workshops & Courses
      • Teaching Modules
    • Research Support Services
      • Field Site Coordination
      • Letters of Support
      • Mobile Deployment Platforms
      • Permits and Permissions
      • AOP Flight Campaigns
      • Research Support FAQs
      • Research Support Projects
    • Funding Opportunities

    Resources

  • Get Involved
    • Advisory Groups
      • Science, Technology & Education Advisory Committee
      • Technical Working Groups
    • Upcoming Events
    • NEON Ambassador Program
      • Exploring NEON-Derived Data Products Workshop Series
    • Research and Collaborations
      • Environmental Data Science Innovation and Inclusion Lab
      • Collaboration with DOE BER User Facilities and Programs
      • EFI-NEON Ecological Forecasting Challenge
      • NEON Great Lakes User Group
      • NEON Science Summit
      • NCAR-NEON-Community Collaborations
        • NCAR-NEON Community Steering Committee
    • Community Engagement
      • How Community Feedback Impacts NEON Operations
    • Science Seminars and Data Skills Webinars
      • Past Years
    • Work Opportunities
      • Careers
      • Seasonal Fieldwork
      • Internships
        • Intern Alumni
    • Partners

    Get Involved

  • My Account
  • Search

Search

Learning Hub

  • Science Videos
  • Tutorials
  • Workshops & Courses
  • Teaching Modules

Breadcrumb

  1. Resources
  2. Learning Hub
  3. Tutorials
  4. Using an API Token when Accessing NEON Data with neonUtilities

Tutorial

Using an API Token when Accessing NEON Data with neonUtilities

Authors: [Claire K. Lunch]

Last Updated: Dec 31, 2024

NEON data can be downloaded from either the NEON Data Portal or the NEON API. When downloading from the Data Portal, you can create a user account. Read about the benefits of an account on the User Account page. You can also use your account to create a token for using the API. Your token is unique to your account, so don鈥檛 share it.

Using a token is optional! You can download data without a token, and without a user account. Using a token when downloading data via the API, including when using the neonUtilities package, links your downloads to your user account, as well as enabling faster download speeds. For more information about token usage and benefits, see the page.

For now, in addition to faster downloads, using a token helps NEON to track data downloads. Using anonymized user information, we can then calculate data access statistics, such as which data products are downloaded most frequently, which data products are downloaded in groups by the same users, and how many users in total are downloading data. This information helps NEON to evaluate the growth and reach of the observatory, and to advocate for training activities, workshops, and software development.

Tokens can be used whenever you use the NEON API. In this tutorial, we鈥檒l focus on using tokens with the neonUtilities R package and the neonutilities Python package. You can follow the tutorial using your preferred programming language.

Objectives

After completing this activity, you will be able to:

  • Create a NEON API token
  • Use your token when downloading data with neonUtilities

Things You鈥檒l Need To Complete This Tutorial

A recent version of R (version 4+) or Python (3.9+) installed on your computer.

Install and Load Packages

R

Install the neonUtilities package. You can skip this step if it鈥檚 already installed, but remember to update regularly.

install.packages("neonUtilities")

Load the package.

library(neonUtilities)

Python

Install the neonutilities package. You can skip this step if it鈥檚 already installed, but remember to update regularly.

# do this in the command line
pip install neonutilities

Load the package.


import neonutilities as nu
import os

Additional Resources

  • NEON
  • neonUtilities tutorial

If you鈥檝e never downloaded NEON data using the neonUtilities package before, we recommend starting with the Download and Explore tutorial before proceeding with this tutorial.

In the next sections, we鈥檒l get an API token from the NEON Data Portal, and then use it in neonUtilities when downloading data.

Get a NEON API Token

The first step is create a NEON user account, if you don鈥檛 have one. Follow the instructions on the Data Portal User Accounts page. If you do already have an account, go to , sign in, and go to your My Account profile page.

Once you have an account, you can create an API token for yourself. At the bottom of the My Account page, you should see this bar:

Click the 鈥楪ET API TOKEN鈥� button. After a moment, you should see this:

Click on the Copy button to copy your API token to the clipboard:

Use API token in neonUtilities

In the next section, we鈥檒l walk through saving your token somewhere secure but accessible to your code. But first let鈥檚 try out using the token the easy way, by using it as a simple text string.

NEON API tokens are very long, so it would be annoying to keep pasting the entire string into functions. Assign your token an object name:

R

NEON_TOKEN <- "PASTE YOUR TOKEN HERE"

Now we鈥檒l use the loadByProduct() function to download data. Your API token is entered as the optional token input parameter. For this example, we鈥檒l download Plant foliar traits (DP1.10026.001).

foliar <- loadByProduct(dpID="DP1.10026.001", site="all", 
                        package="expanded", check.size=F,
                        token=NEON_TOKEN)

Python


NEON_TOKEN = "PASTE YOUR TOKEN HERE"

Now we鈥檒l use the load_by_product() function to download data. Your API token is entered as the optional token input parameter. For this example, we鈥檒l download Plant foliar traits (DP1.10026.001).

foliar = nu.load_by_product(dpid="DP1.10026.001", site="all", 
                        package="expanded", check_size=False,
                        token=NEON_TOKEN)

You should now have data saved in the foliar object; the API silently used your token. If you鈥檝e downloaded data without a token before, you may notice this is faster!

This format applies to all neonUtilities functions that involve downloading data or otherwise accessing the API; you can use the token input with all of them. For example, when downloading remote sensing data:

Use token to download AOP data

R

chm <- byTileAOP(dpID="DP3.30015.001", site="WREF", 
                 year=2017, check.size=F,
                 easting=c(571000,578000), 
                 northing=c(5079000,5080000), 
                 savepath=getwd(),
                 token=NEON_TOKEN)

Python

chm = nu.by_tile_aop(dpid="DP3.30015.001", site="WREF", 
                 year=2017, check_size=False,
                 easting=[571000,578000], 
                 northing=[5079000,5080000], 
                 savepath=os.getcwd(),
                 token=NEON_TOKEN)

Token management for open code

Your API token is unique to your account, so don鈥檛 share it!

If you鈥檙e writing code that will be shared with colleagues or available publicly, such as in a GitHub repository or supplemental materials of a published paper, you can鈥檛 include the line of code above where we assigned your token to NEON_TOKEN, since your token is fully visible in the code there. Instead, you鈥檒l need to save your token locally on your computer, and pull it into your code without displaying it. There are a few ways to do this, we鈥檒l show two options here.

  • Option 1: Save the token in a local file, and source() (R) or import (Python) that file at the start of every script. This is fairly simple but requires a line of code in every script.

  • Option 2: Set the token as an environment variable and you can access it from any script. This is a little harder to set up initially, but once it鈥檚 done, it鈥檚 done globally, and it will work in every script you run.

Option 1: Save token in a local file

R

Open a new, empty R script (.R). Put a single line of code in the script:

NEON_TOKEN <- "PASTE YOUR TOKEN HERE"

Save this file in a logical place on your machine, somewhere that won鈥檛 be visible publicly. Here, let鈥檚 call the file neon_token_source.R, and save it to the working directory. Then, at the start of every script where you鈥檙e going to use the NEON API, you would run this line of code:

source(paste0(wd, "/neon_token_source.R"))

Now you can use token=NEON_TOKEN when you run neonUtilities functions, and you can share your code without accidentally sharing your token.

Python

Open a new, empty Python script (.py). Put a single line of code in the script:


NEON_TOKEN = "PASTE YOUR TOKEN HERE"

Save this file in a logical place on your machine, somewhere that won鈥檛 be visible publicly. Here, let鈥檚 call the file neon_token_source.py, and save it to the working directory. Then, at the start of every script where you鈥檙e going to use the NEON API, you would run this line of code:

import neon_token_source

Now you can use token=neon_token_source.NEON_TOKEN when you run neonutilities functions, and you can share your code without accidentally sharing your token.

Option 2: Set token as environment variable

R

To create a persistent environment variable in R, we use a .Renviron file. Before creating a file, check which directory R is using as your home directory:

# For Windows:
Sys.getenv("R_USER")

For Mac/Linux:

Sys.getenv("HOME")

Check the home directory to see if you already have a .Renviron file, using the file browse pane in RStudio, or using another file browse method with hidden files shown. Files that begin with . are hidden by default, but RStudio recognizes files that begin with .R and displays them.

Screenshot of file browse pane with .Renviron file.

If you already have a .Renviron file, open it and follow the instructions below to add to it. If you don鈥檛 have one, create one using File -> New File -> Text File in the RStudio menus.

Add one line to the text file. In this case, there are no quotes around the token value.

NEON_TOKEN=PASTE YOUR TOKEN HERE

Save the file as .Renviron, in the RStudio home directory identified above. Double check the spelling, this will not work if you have a typo. Re-start R to load the environment.

Once your token is assigned to an environment variable, use the function Sys.getenv() to access it. For example, in loadByProduct():

foliar <- loadByProduct(dpID="DP1.10026.001", site="all", 
                        package="expanded", check.size=F,
                        token=Sys.getenv("NEON_TOKEN"))

Python

To create a persistent environment variable in Python, the simplest option is to use the dotenv module. You will still need to load the variables in each script, but it provides a more flexible way to manage enrionment variables.

pip install python-dotenv

Create a file named .env in the project folder. If you鈥檙e using GitHub, make sure .env is in your .gitignore to avoid syncing tokens to GitHub.

To add variables to the .env file:

import dotenv

dotenv.set_key(dotenv_path=".env", key_to_set="NEON_TOKEN", value_to_set="YOUR TOKEN HERE")

Use the command dotenv.load_dotenv() to load environment variables to the session, then use os.environ.get() to access particular variables. For example, in load_by_product():

dotenv.load_dotenv()
foliar = nu.load_by_product(dpid="DP1.10026.001", site="all", 
                        package="expanded", check_size=False,
                        token=os.environ.get("NEON_TOKEN"))

If dotenv.load_dotenv() returns False, the variables did not load. Try dotenv.load_dotenv(dotenv.find_dotenv(usecwd=True)).

Get Lesson Code

neon-api-tokens-tutorial.R

Questions?

If you have questions or comments on this content, please contact us.

Contact Us
NSF NEON, Operated by Battelle

Follow Us:

Join Our Newsletter

Get updates on events, opportunities, and how NEON is being used today.

Subscribe Now

Footer

  • AG真人百家乐官方网站 Us
  • Newsroom
  • Contact Us
  • Terms & Conditions
  • Careers
  • Code of Conduct

Copyright © Battelle, 2025

The National Ecological Observatory Network is a major facility fully funded by the U.S. National Science Foundation.

Any opinions, findings and conclusions or recommendations expressed in this material do not necessarily reflect the views of the U.S. National Science Foundation.