fromDotenv

Learn how to create the SDK from a .env file.

This static factory method creates an SDK instance from a .env file, the simplest way to get started.

It reads the credentials from the CROCT_* variables in the .env file, falling back to the process environment, and uses a cookie-based session store, so there is nothing to wire up.

Signature

This method has the following signature:

public static function fromDotenv(?string $directory = null, ?IdentityStore $storage = null): self

This method returns a configured SDK instance.

Example

Here is a basic example of how to use this method:

1234
<?phpuse Croct\Plug\Croct;
$croct = Croct::fromDotenv();

Environment variables

The method reads the following variables:

VariableRequiredDescription
CROCT_APP_IDYesThe ID of the application.
CROCT_API_KEYYesThe API key used to authenticate requests.
CROCT_TOKEN_DURATIONNoThe lifetime of the user token, in seconds.
CROCT_BASE_ENDPOINT_URLNoThe base URL to use for the API calls.

Parameters

The following list describes the supported parameters:

directory(optional)
string

The directory containing the .env file.

The default is null, which uses the current working directory.

Default:null
storage(optional)

The storage that persists the visitor session.

The default is null, which uses a cookie-based store.

Default:null