# plug

Learn how to create and configure the SDK.

This static factory method creates an SDK instance wired with sensible defaults.

The HTTP client and [PSR-17](https://www.php-fig.org/psr/psr-17/) factories are [auto-discovered](https://github.com/php-http/discovery) when not provided.

## Signature

This method has the following signature:

```php
public static function plug(
    string $appId,
    ApiKey|string $apiKey,
    IdentityStore $storage,
    ?IdentityResolver $identity = null,
    ?string $baseEndpointUrl = null,
    int $tokenDuration = 86400,
    bool $debug = false,
    ?ContentProvider $contentProvider = null,
    ?RequestContext $context = null,
    ?ClientInterface $httpClient = null,
    ?RequestFactoryInterface $requestFactory = null,
    ?StreamFactoryInterface $streamFactory = null,
    ?LoggerInterface $logger = null,
): self
```

This method returns a configured SDK instance.

## Example

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

```php
<?php
use Croct\Plug\Croct;
use Croct\Plug\CookieStorage;

$croct = Croct::plug(
    appId: 'APPLICATION_ID',
    apiKey: 'API_KEY',
    storage: CookieStorage::fromGlobals(),
);
```

## Parameters

The following list describes the supported parameters:

- `appId`: `string`

  The ID of the application, available on the [Integration page](https://app.croct.com/redirect/organizations/-organization-/workspaces/-workspace-/applications/-application-/integration) of your application.

- `apiKey`: `ApiKey|string` (see [ApiKey](/reference/sdk/php/api/authentication/api-key))

  The API key used to authenticate server-side requests, passed as a serialized string or an `ApiKey` instance.

  You can create one on the [Integration page](https://app.croct.com/redirect/organizations/-organization-/workspaces/-workspace-/applications/-application-/integration) of your application. When the key carries a private key, the SDK issues [signed tokens](/explanation/application/signed-tokens) for the visitor.

- `storage`: [`CookieStorage`](/reference/sdk/php/api/storage/cookie-storage)

  The storage that persists the visitor session, such as the client ID and user token.

- `identity`: [`IdentityResolver`](/reference/sdk/php/api/resolvers/identity-resolver) (optional)

  A resolver that keeps the visitor in sync with the authenticated user of your application.

- `baseEndpointUrl`: `string` (optional) (default: null)

  The base URL to use for the API calls.

  The default is `null`, which uses the production endpoint. This option is helpful for testing, allowing you to point the SDK to a [mock server](/reference/sdk/php/testing/integration-testing).

- `tokenDuration`: `int` (optional) (default: 86400)

  The lifetime of the user token, in seconds.

- `debug`: `bool` (optional) (default: false)

  Whether to enable [debug mode](/reference/sdk/javascript/troubleshooting/debugging#enable-debug-mode) on the client-side SDK.

  When enabled, the browser SDK logs detailed information to the console.

- `contentProvider`: [`ContentProvider`](/reference/sdk/php/api/content/content-provider) (optional) (default: null)

  A source of default content used as fallback when dynamic content is unavailable.

  Auto-discovered from the committed content when not set.

- `context`: `RequestContext` (optional) (default: null)

  The request signals used to personalize content, such as the URL, referrer, IP, user agent, and preferred locale.

  Built from the PHP superglobals when not set.

- `httpClient`: [`ClientInterface`](https://www.php-fig.org/psr/psr-18/#clientinterface) (optional) (default: null)

  A [PSR-18](https://www.php-fig.org/psr/psr-18/) HTTP client used to call the Croct API.

  Auto-discovered when not set.

- `requestFactory`: [`RequestFactoryInterface`](https://www.php-fig.org/psr/psr-17/#21-requestfactoryinterface) (optional) (default: null)

  A [PSR-17](https://www.php-fig.org/psr/psr-17/) request factory.

  Auto-discovered when not set.

- `streamFactory`: [`StreamFactoryInterface`](https://www.php-fig.org/psr/psr-17/#24-streamfactoryinterface) (optional) (default: null)

  A [PSR-17](https://www.php-fig.org/psr/psr-17/) stream factory.

  Auto-discovered when not set.

- `logger`: [`LoggerInterface`](https://www.php-fig.org/psr/psr-3/#3-psrlogloggerinterface) (optional)

  A [PSR-3](https://www.php-fig.org/psr/psr-3/) logger that records transport failures.
