# Infisical::SDK

The official Perl SDK for [Infisical](https://infisical.com), the open-source secrets management platform.

```perl
use Infisical::SDK;

my $infisical = Infisical::SDK->new->auth->universal_auth->login(
    client_id     => $ENV{INFISICAL_UNIVERSAL_AUTH_CLIENT_ID},
    client_secret => $ENV{INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET},
);

my $db_url = $infisical->secrets->get(
    project_id  => $ENV{INFISICAL_PROJECT_ID},
    environment => 'prod',
    secret_name => 'DATABASE_URL',
)->secret_value;
```

## Features

- **Universal Auth** login, with credentials read from the environment when you don't pass them.
- **Secrets**: create, update, get by name, list a folder, and delete.
- **Effective secret lists**: `list` merges secrets from [secret imports](https://infisical.com/docs/documentation/platform/secret-reference) and, in recursive mode, removes duplicate keys so you get exactly one value per name.
- **`%ENV` export**: `attach_to_env => 1` loads a folder into the environment, so child processes inherit it.
- **Strict arguments**: every method takes named arguments and rejects unknown ones, so a typo fails loudly instead of quietly reading the wrong folder.
- **Exceptions**: failures are `Infisical::SDK::Error` objects you can inspect, with HTTP status, request ID, and the line of your code that made the call.
- **Safe retries**: the SDK retries rate-limited (429) requests and temporary network errors, such as a refused connection or a timeout, with exponential backoff. It never applies a change twice.
- **Few dependencies**: built on [Moo](https://metacpan.org/pod/Moo), [Type::Tiny](https://metacpan.org/pod/Type::Tiny) and [HTTP::Tiny](https://metacpan.org/pod/HTTP::Tiny). Perl 5.14 or later.

## Installation

Each [GitHub release](https://github.com/Infisical/perl-sdk/releases) has the distribution tarball attached. `cpanm` installs it and its dependencies:

```sh
cpanm https://github.com/Infisical/perl-sdk/releases/download/v0.001/Infisical-SDK-0.001.tar.gz
```

Infisical::SDK is not on CPAN yet. To install the latest code from the `main` branch:

```sh
cpanm https://github.com/Infisical/perl-sdk.git
```

Or from a checkout:

```sh
cpanm --installdeps .
perl Makefile.PL && make && make test && make install
```

HTTPS needs [IO::Socket::SSL](https://metacpan.org/pod/IO::Socket::SSL), which `cpanm` installs as a dependency.

## Usage

### Create a client and log in

```perl
use Infisical::SDK;

my $infisical = Infisical::SDK->new(
    site_url => 'https://app.infisical.com',   # default; use https://eu.infisical.com or your own instance
);

$infisical->auth->universal_auth->login(
    client_id     => $client_id,
    client_secret => $client_secret,
);
```

If you leave out `client_id` or `client_secret`, `login` reads `INFISICAL_UNIVERSAL_AUTH_CLIENT_ID` and `INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET`. `login` returns the client, so construction and login can be one statement:

```perl
my $infisical = Infisical::SDK->new->auth->universal_auth->login;
```

To create a Universal Auth identity, see [Universal Auth](https://infisical.com/docs/documentation/platform/identities/universal-auth).

### Create a secret

```perl
my $secret = $infisical->secrets->create(
    project_id     => $project_id,
    environment    => 'dev',
    secret_path    => '/backend',          # defaults to /
    secret_name    => 'STRIPE_KEY',
    secret_value   => 'sk_test_123',
    secret_comment => 'Test mode key',
);
```

### Get a secret by name

```perl
my $secret = $infisical->secrets->get(
    project_id  => $project_id,
    environment => 'dev',
    secret_path => '/backend',
    secret_name => 'STRIPE_KEY',
);

say $secret->secret_value;
```

Pass `version => 3` to read an older version.

### Update a secret

```perl
$infisical->secrets->update(
    project_id      => $project_id,
    environment     => 'dev',
    secret_path     => '/backend',
    secret_name     => 'STRIPE_KEY',
    secret_value    => 'sk_test_456',
    new_secret_name => 'STRIPE_SECRET_KEY',  # optional rename
);
```

Only the fields you pass change.

### Delete a secret

```perl
my $deleted = $infisical->secrets->delete(
    project_id  => $project_id,
    environment => 'dev',
    secret_path => '/backend',
    secret_name => 'STRIPE_SECRET_KEY',
);
```

`delete` returns the secret as it was before the delete, and throws a 404 `Infisical::SDK::Error::Request` if the folder has no such secret. A secret that the folder only gets through a secret import has to be deleted in the folder it comes from.

### List the secrets in a folder

```perl
my @secrets = $infisical->secrets->list(
    project_id  => $project_id,
    environment => 'prod',
    secret_path => '/backend',
    recursive   => 1,
);

for my $secret (@secrets) {
    printf "%s=%s (from %s)\n", $secret->secret_key, $secret->secret_value, $secret->secret_path;
}
```

`list` returns the folder's effective secrets, one per key:

- Secrets pulled in through secret imports are always included. A secret defined in the folder wins over an imported one, and a later import wins over an earlier one, which is the order Infisical itself resolves a secret in.
- With `recursive => 1`, subfolders are included. The Infisical API returns every copy of a key that appears in more than one folder; the SDK keeps the one closest to `secret_path`. Between folders at the same depth, the folder whose path sorts first wins (`/app/api` beats `/app/worker`).

### Load secrets into `%ENV`

```perl
$infisical->secrets->list(
    project_id    => $project_id,
    environment   => 'prod',
    secret_path   => '/backend',
    recursive     => 1,
    attach_to_env => 1,
);

system 'bin/migrate';   # sees every secret as an environment variable
```

Existing variables with the same name are overwritten. If the identity can see a secret but not read its value, `list` throws instead of exporting a placeholder, and `%ENV` is left unchanged. Use `local %ENV = %ENV;` first to scope the change to a block. [`examples/run-with-secrets.pl`](examples/run-with-secrets.pl) wraps any command this way.

### Handle errors

Every failure throws an `Infisical::SDK::Error`:

| Class | When |
| --- | --- |
| `Infisical::SDK::Error::Validation` | A method got a missing, unknown, or invalid argument. Nothing was sent. |
| `Infisical::SDK::Error::Request` | Infisical returned an error status. Has `status_code`, `request_id`, and `body`. |
| `Infisical::SDK::Error::Network` | The request got no response (connection refused, TLS failure, timeout). |
| `Infisical::SDK::Error::ApprovalRequired` | The folder has a change approval policy, so Infisical opened an approval request instead. Has `approval`. |
| `Infisical::SDK::Error` (no subclass) | The response is not what the SDK expects, such as an HTML page from a proxy or a login response with no access token. Also thrown when `attach_to_env` cannot read the value of a secret. |

```perl
use Scalar::Util qw(blessed);

my $secret = eval { $infisical->secrets->get(%args) };
if ( my $err = $@ ) {
    die $err unless blessed $err && $err->isa('Infisical::SDK::Error::Request') && $err->status_code == 404;
    $secret = $infisical->secrets->create( %args, secret_value => generate_password() );
}
```

Uncaught, an error reads like a normal `die`:

```
[URL=https://app.infisical.com/api/v4/secrets/STRIPE_KEY?...] [Method=GET] [StatusCode=404] Secret with name 'STRIPE_KEY' not found at app.pl line 12.
```

### Client options

| Option | Default | Description |
| --- | --- | --- |
| `site_url` | `https://app.infisical.com` | Base URL of your Infisical instance, starting with `http://` or `https://`. |
| `timeout` | `10` | Seconds before a request times out. |
| `max_retries` | `4` | Retries after a 429 or a temporary network error, with exponential backoff. |
| `ua` | an `HTTP::Tiny` | Any object with HTTP::Tiny's `request` method, for proxies or custom TLS. |

Full reference: `perldoc Infisical::SDK`, `perldoc Infisical::SDK::Secrets`.

## Development

```sh
cpanm --installdeps --with-develop .
prove -lr t                 # unit tests, no network
prove -lr xt/author         # POD, Perl::Critic, perltidy
```

`xt/live` runs the SDK against a real Infisical instance. It needs a Universal Auth identity with admin access to a project that has `dev` and `staging` environments, and creates everything it uses under a new folder:

```sh
export INFISICAL_SITE_URL=http://localhost:8080
export INFISICAL_PROJECT_ID=...
export INFISICAL_UNIVERSAL_AUTH_CLIENT_ID=...
export INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET=...
prove -l xt/live
```

CI runs `xt/live` on pushes to `main` and before each release, with the repository secrets of the same names. Without the secrets, the tests are skipped.

### Releasing

1. Set the new version in `lib/Infisical/SDK.pm`. It is the only `$VERSION` in the distribution. Use a decimal version, such as `0.002`. A version with an underscore, such as `0.002_01`, is a trial release: GitHub marks it as a pre-release, and CPAN does not index it.
2. At the top of `Changes`, add an entry with the version and the release date, such as `0.002  2026-12-01`, and list the changes under it.
3. Merge the change to `main`.
4. Check the release, then tag the merged commit and push the tag:

   ```sh
   perl maint/check-release v0.002
   git tag v0.002
   git push origin v0.002
   ```

   You can also create the tag when you create the release on GitHub.

The tag starts the release workflow. The workflow runs CI on the tagged commit, builds `Infisical-SDK-0.002.tar.gz`, and attaches it to the GitHub release for the tag. If that release does not exist, the workflow creates it, with the `Changes` entry as the release notes.

When the repository has the `PAUSE_TOKEN` secret, the workflow also uploads the tarball to CPAN. Until then, it skips the upload. To get a token, log in to [PAUSE](https://pause.perl.org) and generate one.

## License

MIT. See [LICENSE](LICENSE).
