Celerium.BirdWeather
About The Project
The Celerium.BirdWeather PowerShell wrapper offers a way to read & write data in the BirdWeather REST API (v1) and to query the BirdWeather GraphQL API with one module. This module serves to abstract away the details of interacting with BirdWeather’s APIs in such a way that is consistent with PowerShell nomenclature. This gives bird watchers, system administrators, and PowerShell developers a convenient and familiar way of using BirdWeather’s APIs to create documentation scripts, automation, dashboards, and integrations.
- :book: Celerium.BirdWeather project documentation can be found on Github Pages
- :book: BirdWeather’s REST API documentation is hosted at app.birdweather.com/api/v1
- :book: BirdWeather’s GraphQL API documentation is hosted at app.birdweather.com/api/index.html
Celerium.BirdWeather replaces the Celerium.BirdWeatherREST & Celerium.BirdWeatherGraphQL modules. Both APIs share one base URI, one set of credentials, one configuration file, & one set of environment variables.
| API | Functions | Generated from | Aliases |
|---|---|---|---|
| REST | *-BirdWeatherREST* | The OpenAPI spec (docs/BirdWeatherREST-1.20261114.json) | *-BWR* |
| GraphQL | *-BirdWeatherGraphQL* | The introspection result of the GraphQL schema (docs/BirdWeatherGraphQL.json) | *-BWG* |
Both files are also included in the built module and every endpoint & operation is tracked in the endpoints.csv file.
:information_source: The OpenAPI spec is an unofficial description of the BirdWeather REST API, it is not published or endorsed by BirdWeather.
In order to maintain PowerShell best practices, only approved verbs are used and PowerShell’s verb-noun nomenclature is respected. Each noun is prefixed with BirdWeatherREST or BirdWeatherGraphQL in an attempt to prevent naming problems. The functions that are shared by both APIs (Add-BirdWeatherAPIKey, Add-BirdWeatherBaseURI, …) are prefixed with BirdWeather.
- REST GET ->
Get-, POST ->New-, PUT\PATCH ->Set-, DELETE ->Remove- - GraphQL queries ->
Get-, mutations ->New-,Set-, &Remove-(the schema does not have mutations) - All commands, parameters, & variables use PascalCase
- Every function (public & private) also has a short alias,
Get-BWRStationStats(REST),Get-BWGStations(GraphQL),Add-BWAPIKey(shared) - Internally every request goes through
Invoke-BirdWeatherRequest -Api REST|GraphQL, which passes it to the request function of the API
Install
This module can be installed directly from the PowerShell Gallery with the following command:
Install-Module -Name Celerium.BirdWeather
- :information_source: This module supports Windows PowerShell 5.1 and PowerShell 7+ on Windows, Linux, & macOS. Every change is tested on all of them by the CI workflow.
- :information_source: If you are running an older version of PowerShell, or if PowerShellGet is unavailable, you can manually download the main branch and place the latest version of Celerium.BirdWeather from the build folder into the (default)
C:\Program Files\WindowsPowerShell\Modulesfolder.
Celerium.BirdWeather project documentation can be found on Github Pages
- A full list of functions can be retrieved by running
Get-Command -Module Celerium.BirdWeather. - Help info and a list of parameters can be found by running
Get-Help <command name>, such as:
Get-Help Get-BirdWeatherRESTStationDetections
Get-Help Get-BirdWeatherGraphQLStationDetections -Full
- Use wildcards (or the
BWR&BWGaliases) to find the function for an endpoint:
Get-Command -Module Celerium.BirdWeather -Name *Soundscape*
Get-Command -Name *-BWR*
Get-Command -Name *-BWG*
Initial Setup
After installing this module, you will need to configure your API data that is used to talk with the BirdWeather APIs. The GraphQL API is public, the module works with it without any configuration.
- [optional] Run
Add-BirdWeatherBaseURI- The BirdWeather production host
https://app.birdweather.comis used by default, onlyhttpsis supported - The REST URI (
/api/v1) & the GraphQL URI (/graphql) are created from the host. A REST or GraphQL url is converted to the host, such ashttps://app.birdweather.com/api/v1->https://app.birdweather.com Get-BirdWeatherBaseURI -Api REST&Get-BirdWeatherBaseURI -Api GraphQLshow the full URIs
- The BirdWeather production host
- Run
Add-BirdWeatherAPIKey-StationToken(or-StationTokenSecureString) is the station token of the REST API. It is used as the default-Tokenof every station function, such asGet-BirdWeatherRESTStationStats. Without a parameter the cmdlet prompts for it as a secure string-IsAdminTokenstores the station token as an administrator API token, it is sent in theAuthorizationheader and grants full access to every station. When it is used, set-Tokento the station ID (such as21934). Treat it like a password-ApiKey(or-ApiKeySecureString) is the optional Bearer API key of the GraphQL API, the public data can be read without one- Either one or both can be used:
Add-BirdWeatherAPIKey -StationToken 'token' -ApiKey 'key' - Public stations can also be read with their numeric station ID (
-Token 21934) without a station token. ThePUC-21934style name shown in the BirdWeather app is not accepted by the APIs - Functions that do not need a station (
Get-BirdWeatherRESTSpeciesById,Get-BirdWeatherRESTDetectionsById,New-BirdWeatherRESTSpeciesLookup, & every GraphQL function) work without a token
- [optional] Run
Test-BirdWeatherAPIKey- Makes a single request to validate the base uri & credentials. The REST API is tested (
GET /stations/{token}/stats) when a station token is set, otherwise the GraphQL API (query { __typename }). Use-Api RESTor-Api GraphQLto choose
- Makes a single request to validate the base uri & credentials. The REST API is tested (
- [optional] Run
Export-BirdWeatherModuleSettings- This will create a config file at
%UserProfile%\Celerium.BirdWeather(~/.Celerium.BirdWeatheron Linux & macOS) that holds the base uri, station token, & API key information. - Next time you run
Import-Module -Name Celerium.BirdWeather, this configuration file will automatically be loaded. - :warning: Exporting module settings encrypts your secrets in a format that can only be unencrypted by the user principal that encrypted the secret. It makes use of .NET DPAPI, which for Windows uses reversible encrypted tied to your user principal. This means that you cannot copy your configuration file to another computer or user account and expect it to work.
- :warning: However in Linux\Unix operating systems the secret keys are more obfuscated than encrypted so it is recommend to use environment variables or a more secure & cross-platform storage method.
- This will create a config file at
Environment variables (recommended for CI\CD, Linux, & macOS) are loaded when the module is imported and take precedence over the configuration file:
| Variable | Description |
|---|---|
BIRDWEATHER_BASE_URI | The BirdWeather host (a REST or GraphQL url is converted to the host) |
BIRDWEATHER_STATION_TOKEN | The station token of the REST API |
BIRDWEATHER_ADMIN_TOKEN | An administrator API token of the REST API, used instead of the station token |
BIRDWEATHER_API_KEY | The Bearer API key of the GraphQL API |
$env:BIRDWEATHER_STATION_TOKEN = 'your-station-token'
# $env:BIRDWEATHER_ADMIN_TOKEN = 'your-administrator-token' (used instead of the station token)
# $env:BIRDWEATHER_API_KEY = 'your-graphql-api-key'
# $env:BIRDWEATHER_BASE_URI = 'https://app.birdweather.com'
Import-Module -Name Celerium.BirdWeather
:information_source: The station token is part of the REST request path, the module never writes it to the verbose output, error messages, or the debug variables.
Usage REST
Calling a REST endpoint is as simple as running its Get-BirdWeatherREST* function. Station functions use the station token that was stored with Add-BirdWeatherAPIKey -StationToken unless -Token is used.
# Detection & species counts of the last week
Get-BirdWeatherRESTStationStats -Period week
# The 10 most detected species of the last month
Get-BirdWeatherRESTStationSpecies -Period month -Limit 10
# Every detection of the last day, 100 at a time
Get-BirdWeatherRESTStationDetections -From (Get-Date).AddDays(-1) -AllResults
# Get a single detection by id & get the full response
Get-BirdWeatherRESTStationDetections -Id 12297900660 -Raw
# Public data that does not need a token
Get-BirdWeatherRESTSpeciesById -Id 120
# Another station by using its station ID (public stations or an administrator API token)
Get-BirdWeatherRESTStationStats -Token 21934
- Function names are built from the endpoint path, such as
/stations/{token}/speciesisGet-BirdWeatherRESTStationSpecies - An endpoint & its single item endpoint (
/stations/{token}/detections&/stations/{token}/detections/{id}) with the same method are one function that uses the parameter setsList&ById.Get-BirdWeatherRESTStationDetectionslists detections &Get-BirdWeatherRESTStationDetections -Id 12297900660gets one detection. The old names such asGet-BirdWeatherRESTStationDetectionsByIdstill work as aliases - Parameter names match the names used by the API, such as
SpeciesId,Limit, &CommonName. Parameters that accept more than one value use arrays,-Classification bat,mammal - Dates are real dates,
-From (Get-Date).AddDays(-1), and are sent to the API in UTC (ISO 8601) - The properties of a JSON request body are individual parameters,
New-BirdWeatherRESTStationDetections -CommonName 'Blue Jay' ... - Endpoints that support the
cursororpageparameters also have an-AllResultsswitch that pages through the results for you - By default only the results of a response are returned, use
-Rawto get the full response including thesuccessflag
Usage GraphQL
Querying the GraphQL API is as simple as running its Get-BirdWeatherGraphQL* function. The functions return the data of the query, only the properties that are needed are requested from the API.
# The counts of the whole network
Get-BirdWeatherGraphQLCounts
# One station with only the properties that are needed
Get-BirdWeatherGraphQLStation -StationId 21934 -Property 'id', 'name', 'coords.lat', 'coords.lon'
# The latest detections of a station
Get-BirdWeatherGraphQLStationDetections -StationId 21934 -First 10
# The top species of the last 7 days
Get-BirdWeatherGraphQLTopSpecies -Limit 10 -PeriodCount 7 -PeriodUnit day
# Every station of the network, 100 at a time
Get-BirdWeatherGraphQLStations -First 100 -AllResults -MaxResults 500
# A query that is not covered by the functions
Get-BirdWeatherGraphQLQuery -Query 'query($id: ID!) { station(id: $id) { name } }' -Variable @{ id = '21934' }
- A function is generated for every query of the schema, and for the fields of the returned objects that have arguments (such as
Get-BirdWeatherGraphQLStationDetections) -Propertyselects the properties (GraphQL fields) to return, nested properties use a dot,species.commonName- Connections (paged results) return the nodes.
-AllResultsfollows the pagination cursor,-MaxResultslimits the number of nodes, &-Rawreturns the full response including thepageInfo&totalCount -
Get-BirdWeatherGraphQLQuery(Public/GraphQL/Custom) is written by hand, it sends a GraphQL document as is. Mutations & subscriptions are not sent by it - The full list of operations & their functions is tracked in the endpoints.csv file and on Github Pages.
- Example scripts can be found in the examples folder of this repository. They only use read-only requests.
Write functions
The REST New-, Set-, & Remove- functions change data on your BirdWeather station, with the exception of New-BirdWeatherRESTSpeciesLookup which only looks up species information. The BirdWeather GraphQL schema does not have mutations & a GraphQL mutation is never sent.
- Every write function supports
-WhatIf, which shows what would be sent without sending anything - Every write function asks for confirmation before a request is sent (
ConfirmImpact = 'High'), use-Confirm:$falsein scripts that should not prompt. The species lookup supports-WhatIfbut never asks for confirmation - A write request is never retried automatically
- The unit tests, the live tests, & the examples never send a write request
# Shows the request that would be sent, nothing is sent
New-BirdWeatherRESTStationDetections -Timestamp (Get-Date) -CommonName 'Blue Jay' -ScientificName 'Cyanocitta cristata' -WhatIf
Supported functions
REST
The module has 10 public REST functions in 4 categories that cover all 12 endpoints in the spec. 6 are read-only Get- functions & 4 are New- functions (detections, soundscape upload, station configuration, & species lookup).
| Category | EndpointUri | Method | Function | Example |
|---|---|---|---|---|
| Detections | /detections/{id} | GET | Get-BirdWeatherRESTDetectionsById | |
| Species | /species/lookup | POST | New-BirdWeatherRESTSpeciesLookup | |
| Species | /species/{id} | GET | Get-BirdWeatherRESTSpeciesById | |
| Stations | /stations/{token}/config | POST | New-BirdWeatherRESTStationConfig | |
| Detections | /stations/{token}/detections/{id} | GET | Get-BirdWeatherRESTStationDetections (ById) | Invoke-DetectionExportExample |
| Detections | /stations/{token}/detections | GET | Get-BirdWeatherRESTStationDetections | Invoke-DetectionExportExample |
| Detections | /stations/{token}/detections | POST | New-BirdWeatherRESTStationDetections | |
| Soundscapes | /stations/{token}/soundscapes/{id} | GET | Get-BirdWeatherRESTStationSoundscapes (ById) | |
| Soundscapes | /stations/{token}/soundscapes | GET | Get-BirdWeatherRESTStationSoundscapes | |
| Soundscapes | /stations/{token}/soundscapes | POST | New-BirdWeatherRESTStationSoundscapes | |
| Stations | /stations/{token}/species | GET | Get-BirdWeatherRESTStationSpecies | Invoke-RESTStationOverviewExample |
| Stations | /stations/{token}/stats | GET | Get-BirdWeatherRESTStationStats | Invoke-RESTStationOverviewExample |
GraphQL
The module has 34 public GraphQL functions (33 generated query functions & the custom query function) in 7 categories that cover every query of the schema. Subscriptions need a WebSocket connection & are not supported.
| Category | EndpointUri | Function | Example |
|---|---|---|---|
| Custom | Query (any) | Get-BirdWeatherGraphQLQuery | |
| Species | Query.allSpecies | Get-BirdWeatherGraphQLAllSpecies | |
| Sightings | Query.birdnetSightings | Get-BirdWeatherGraphQLBirdnetSightings | |
| Counts | Query.counts | Get-BirdWeatherGraphQLCounts | Invoke-TopSpeciesExample |
| Detections | Query.dailyDetectionCounts | Get-BirdWeatherGraphQLDailyDetectionCounts | |
| Detections | Query.detections | Get-BirdWeatherGraphQLDetections | |
| Sightings | Query.ebirdSightings | Get-BirdWeatherGraphQLEbirdSightings | |
| Species | Query.searchSpecies | Get-BirdWeatherGraphQLSearchSpecies | |
| Species | Query.species | Get-BirdWeatherGraphQLSpecies | |
| Species | Query.species.detectedStations | Get-BirdWeatherGraphQLSpeciesDetectedStations | |
| Species | Query.species.detectionCounts | Get-BirdWeatherGraphQLSpeciesDetectionCounts | |
| Species | Query.species.latestDetections | Get-BirdWeatherGraphQLSpeciesLatestDetections | |
| Species | Query.species.probabilities | Get-BirdWeatherGraphQLSpeciesProbabilities | |
| Species | Query.species.stations | Get-BirdWeatherGraphQLSpeciesStations | |
| Species | Query.species.topDetections | Get-BirdWeatherGraphQLSpeciesTopDetections | |
| Species | Query.species.translations | Get-BirdWeatherGraphQLSpeciesTranslations | |
| Stations | Query.station | Get-BirdWeatherGraphQLStation | Invoke-GraphQLStationOverviewExample |
| Stations | Query.station.counts | Get-BirdWeatherGraphQLStationCounts | |
| Stations | Query.station.detectionCounts | Get-BirdWeatherGraphQLStationDetectionCounts | |
| Stations | Query.station.detections | Get-BirdWeatherGraphQLStationDetections | Invoke-GraphQLStationOverviewExample |
| Stations | Query.station.sensors | Get-BirdWeatherGraphQLStationSensors | |
| Sensors | Query.station.sensors.accelHistory | Get-BirdWeatherGraphQLStationAccelHistory | |
| Sensors | Query.station.sensors.detectionsHistory | Get-BirdWeatherGraphQLStationDetectionsHistory | |
| Sensors | Query.station.sensors.environmentHistory | Get-BirdWeatherGraphQLStationEnvironmentHistory | |
| Sensors | Query.station.sensors.lightHistory | Get-BirdWeatherGraphQLStationLightHistory | |
| Sensors | Query.station.sensors.locationHistory | Get-BirdWeatherGraphQLStationLocationHistory | |
| Sensors | Query.station.sensors.magHistory | Get-BirdWeatherGraphQLStationMagHistory | |
| Sensors | Query.station.sensors.systemHistory | Get-BirdWeatherGraphQLStationSystemHistory | |
| Stations | Query.station.timeOfDayDetectionCounts | Get-BirdWeatherGraphQLStationTimeOfDayDetectionCounts | |
| Stations | Query.station.topSpecies | Get-BirdWeatherGraphQLStationTopSpecies | Invoke-GraphQLStationOverviewExample |
| Stations | Query.stations | Get-BirdWeatherGraphQLStations | |
| Detections | Query.timeOfDayDetectionCounts | Get-BirdWeatherGraphQLTimeOfDayDetectionCounts | |
| Species | Query.topBirdnetSpecies | Get-BirdWeatherGraphQLTopBirdnetSpecies | |
| Species | Query.topSpecies | Get-BirdWeatherGraphQLTopSpecies | Invoke-TopSpeciesExample |
Moving from Celerium.BirdWeatherREST / Celerium.BirdWeatherGraphQL
Celerium.BirdWeather 1.0.0 combines both modules. Uninstall the old modules or make sure that only one of them is imported in a session. The names of the public functions & their BWR / BWG aliases did not change, scripts that use them keep working after the module name in Import-Module is changed to Celerium.BirdWeather.
| Item | Celerium.BirdWeatherREST | Celerium.BirdWeatherGraphQL | Celerium.BirdWeather |
|---|---|---|---|
| Public functions | *-BirdWeatherREST* | *-BirdWeatherGraphQL* | Unchanged |
| Public aliases | *-BWR* | *-BWG* | Unchanged |
| Base URI | Add-BirdWeatherRESTBaseURI (.../api/v1) | Add-BirdWeatherGraphQLBaseURI (host) | Add-BirdWeatherBaseURI (host), the old names & Add-BWRBaseURI / Add-BWGBaseURI are aliases |
| Station token | Add-BirdWeatherRESTAPIKey -ApiKey | - | Add-BirdWeatherAPIKey -StationToken (-StationTokenSecureString, -IsAdminToken) |
| GraphQL API key | - | Add-BirdWeatherGraphQLAPIKey -ApiKey | Add-BirdWeatherAPIKey -ApiKey (-ApiKeySecureString) |
| Other key & settings functions | *-BirdWeatherREST(APIKey\|BaseURI\|ModuleSettings) | *-BirdWeatherGraphQL(APIKey\|BaseURI\|ModuleSettings) | *-BirdWeather(APIKey\|BaseURI\|ModuleSettings) |
| Configuration folder | Celerium.BirdWeatherREST | Celerium.BirdWeatherGraphQL | Celerium.BirdWeather (run Export-BirdWeatherModuleSettings again) |
| Module variables | BirdWeatherRESTModule* | BirdWeatherGraphQLModule* | BirdWeatherModule* |
-ApiKeymeant something different in each module. In BirdWeatherREST it was the station token, in BirdWeatherGraphQL it was the GraphQL API key. In this module-StationTokenis the station token &-ApiKeyis the GraphQL API key. The old*-BirdWeatherRESTAPIKey&*-BirdWeatherGraphQLAPIKeyfunction names (and their*-BWRAPIKey/*-BWGAPIKeyaliases) are not aliased because of that, update scripts that call them. A value from the pipeline ('token' | Add-BirdWeatherAPIKey) is the station token- The base URI is the host. The REST URI & the GraphQL URI are created from it. A REST url (
.../api/v1) or a GraphQL url (.../graphql) is converted to the host - Environment variables. The new names are
BIRDWEATHER_BASE_URI,BIRDWEATHER_STATION_TOKEN,BIRDWEATHER_ADMIN_TOKEN, &BIRDWEATHER_API_KEY. The old names are still read when the new name is not set:BIRDWEATHERREST_BASE_URI,BIRDWEATHERGRAPHQL_BASE_URI,BIRDWEATHERREST_API_KEY(station token),BIRDWEATHERREST_ADMIN_TOKEN, &BIRDWEATHERGRAPHQL_API_KEY.BIRDWEATHER_API_KEYmeant the GraphQL key before & still does - Configuration file. The module reads
~\Celerium.BirdWeather\config.psd1, the configuration files of the old modules are not read. RunAdd-BirdWeatherAPIKey&Export-BirdWeatherModuleSettingsonce - The module GUID is new & starts with the Celerium
c12prefix
Development
The module is built from the files in this repository with the scripts in the build folder. The same steps are run by the GitHub Actions workflows in .github/workflows on Windows PowerShell 5.1, PowerShell 7+ on Windows, Linux, & macOS.
Celerium.BirdWeather/
Private/Shared/ Request dispatcher, base URI, API keys, & module settings
Private/REST/ REST request function & helpers
Private/GraphQL/ GraphQL request function & helpers
Public/REST/ Generated REST functions (one folder per category)
Public/GraphQL/ Generated GraphQL functions (one folder per category) & Custom (hand written, never generated)
| Script | Description |
|---|---|
| Install-Prerequisites.ps1 | Installs (the latest stable) ModuleBuilder, Pester, Microsoft.PowerShell.PlatyPS, & PSScriptAnalyzer |
| New-PublicFunctions.ps1 | (PowerShell 7+) Runs both generators, then updates the manifest entries & docs/endpoints.csv |
| REST\New-RESTPublicFunctions.ps1 | (PowerShell 7+) Generates Public/REST from the OpenAPI spec |
| GraphQL\New-GraphQLPublicFunctions.ps1 | (PowerShell 7+) Generates Public/GraphQL from the GraphQL schema, never changes Public/GraphQL/Custom |
| Invoke-ModuleBuild.ps1 | Builds the module (psd1, psm1, the OpenAPI spec, & the GraphQL schema) into build/Celerium.BirdWeather/<version> |
| Get-BirdWeatherRESTSpec.ps1 | (PowerShell 7+) Downloads the latest OpenAPI spec to docs/BirdWeatherREST-<version>.json |
| Compare-BirdWeatherRESTSpec.ps1 | Compares the local OpenAPI spec with the online spec (-UpdateLocal replaces it, -FailOnDifference for CI) |
| Get-BirdWeatherGraphQLSpec.ps1 | (PowerShell 7+) Downloads the latest GraphQL schema (introspection) to docs/BirdWeatherGraphQL.json |
| Compare-BirdWeatherGraphQLSpec.ps1 | Compares the local GraphQL schema with the online schema (-UpdateLocal replaces it, -FailOnDifference for CI) |
| Invoke-PesterTests.ps1 | Runs the Pester tests against the source or built module |
| Invoke-HelpContent.ps1 | Regenerates the GitHub pages documentation in docs |
| Publish-CeleriumModule.ps1 | Publishes the built module to the PowerShell Gallery |
./build/Install-Prerequisites.ps1
./build/Invoke-PesterTests.ps1 -Version 1.0.0 -BuildTarget notBuilt
./build/Invoke-ModuleBuild.ps1 -Version 1.0.0
./build/Invoke-PesterTests.ps1 -Version 1.0.0 -BuildTarget built
./build/Invoke-HelpContent.ps1
To support a newer version of an API, compare the local spec with the online spec, download it, & regenerate the functions. The newest docs/BirdWeatherREST-<version>.json file is always used, older files are kept & replaced files (and the files of functions that are no longer generated) are copied to build/Archive (ignored by git):
./build/Compare-BirdWeatherRESTSpec.ps1
./build/Get-BirdWeatherRESTSpec.ps1
./build/Compare-BirdWeatherGraphQLSpec.ps1
./build/Get-BirdWeatherGraphQLSpec.ps1
./build/New-PublicFunctions.ps1
Live tests make real, read-only requests to your station (REST GET requests & GraphQL queries, at most 24 requests, spaced 500 ms apart). They are excluded unless the following environment variables are set and the Live tag is included. The REST tests are skipped without the station token, the GraphQL tests only need the station ID:
$env:BIRDWEATHER_STATION_TOKEN = 'your-station-token'
$env:BIRDWEATHER_STATION_ID = '21934' # the numeric station ID
./build/Invoke-PesterTests.ps1 -Version 1.0.0 -IncludeTag Live -ExcludeTag Online
Contributing
Contributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag “enhancement”. Don’t forget to give the project a star! Thanks again!
See the CONTRIBUTING guide for more information about contributing.
License
Distributed under the MIT license. See LICENSE for more information.