Skip to main content

from_env

Creates a new Growatt client with configuration loaded from environment variables.
pub fn from_env() -> Self

Environment Variables

This method reads the following environment variables:
GROWATT_USERNAME
string
The username for Growatt API authentication
GROWATT_PASSWORD
string
The password for Growatt API authentication
GROWATT_BASE_URL
string
Alternative base URL for the Growatt API. Defaults to https://server.growatt.com if not set
GROWATT_SESSION_DURATION
integer
Session duration in minutes. Defaults to 30 minutes if not set

Returns

Returns a new Growatt instance configured with values from environment variables.

Example

use growatt_api::Growatt;

// Create a .env file with:
// GROWATT_USERNAME=your_username
// GROWATT_PASSWORD=your_password
// GROWATT_SESSION_DURATION=60

let mut client = Growatt::from_env();

// Login automatically uses credentials from environment
client.login(
    &std::env::var("GROWATT_USERNAME").unwrap(),
    &std::env::var("GROWATT_PASSWORD").unwrap()
).await?;
You can set environment variables in a .env file in your project directory. The dotenv crate will automatically load them.

with_alternate_url

Configures the client to use the alternate Growatt API URL.
pub fn with_alternate_url(mut self) -> Self

Returns

Returns self with the base URL set to https://openapi.growatt.com.

Example

use growatt_api::Growatt;

let mut client = Growatt::new()
    .with_alternate_url();

// Now uses https://openapi.growatt.com instead of default URL
client.login("username", "password").await?;
This method uses the builder pattern and returns self, allowing method chaining with other configuration methods.

with_session_duration

Configures the client session duration.
pub fn with_session_duration(mut self, minutes: i64) -> Self

Parameters

minutes
i64
required
The session duration in minutes. Sessions will automatically re-authenticate after this duration expires.

Returns

Returns self with the session duration configured.

Example

use growatt_api::Growatt;

// Configure client with 60-minute session duration
let mut client = Growatt::new()
    .with_session_duration(60);

client.login("username", "password").await?;

Chaining Multiple Configurations

use growatt_api::Growatt;

let mut client = Growatt::new()
    .with_alternate_url()
    .with_session_duration(120);

client.login("username", "password").await?;
The client automatically manages session expiration and will re-authenticate when needed, as long as credentials were provided during the initial login() call.

Build docs developers (and LLMs) love