> ## Documentation Index
> Fetch the complete documentation index at: https://infisical.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Infisical Perl SDK

> Fetch and manage secrets in a Perl application with the official Infisical Perl SDK.

The Infisical Perl SDK, `Infisical::SDK`, lets a Perl application log in to Infisical as a [machine identity](https://infisical.com/docs/documentation/platform/identities/machine-identities.md) and create, update, fetch, list, and delete secrets. It can also store the secrets of a folder in `%ENV`, so that every program your application starts gets the secrets as environment variables.

This page covers the main methods of the SDK. For every method and option, see the [Infisical::SDK documentation on MetaCPAN](https://metacpan.org/pod/Infisical::SDK).

## Prerequisites

* Perl 5.14 or later
* A machine identity that uses [Universal Auth](https://infisical.com/docs/documentation/platform/identities/universal-auth.md), and its client ID and client secret
* The machine identity added to your project with a [role](https://infisical.com/docs/documentation/platform/access-controls/role-based-access-controls.md#project-level-access-controls) that can read secrets, such as **Viewer** (or **Member**, if your application creates, updates, or deletes secrets)

## Installation

```bash theme={"dark"}
cpan install Infisical::SDK
```

## Getting started

```perl theme={"dark"}
use strict;
use warnings;
use Infisical::SDK;

my $client = Infisical::SDK->new(
    site_url => 'https://app.infisical.com', # Optional, this is the default
);

# Log in as the machine identity
$client->auth->universal_auth->login(
    client_id     => '<machine-identity-client-id>',
    client_secret => '<machine-identity-client-secret>',
);

my @secrets = $client->secrets->list(
    project_id  => '<project-id>',
    environment => 'dev', # dev, staging, prod, or a custom environment slug
);

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

If you self-host Infisical, set `site_url` to the URL of your instance. For the EU region of Infisical Cloud, set `site_url` to `https://eu.infisical.com`.

### Client options

`new` takes these options, and each of them is optional:

* `site_url` (string): the URL of your Infisical instance, starting with `http://` or `https://` (defaults to `https://app.infisical.com`)
* `timeout` (number): the number of seconds to wait for each request (defaults to `10`)
* `max_retries` (integer): the number of times to retry a request after Infisical rate-limits it or after a temporary network error, such as a timeout (defaults to `4`)
* `ua` (object): the HTTP client that the SDK sends requests with, such as an [HTTP::Tiny](https://metacpan.org/pod/HTTP::Tiny) object with proxy settings (any object with HTTP::Tiny's `request` method works)

If a request changes a secret, the SDK retries the request only if it never reached Infisical, so a retry can't apply a change twice.

## Core methods

The client's methods are in two groups:

* `auth`: logs the client in
* `secrets`: creates, updates, fetches, lists, and deletes secrets

Every method takes named arguments. Argument names are in snake case, such as `project_id`. If you pass an argument that the method doesn't take, such as `projectId`, the method throws an error and sends nothing to Infisical.

### `auth`

#### Universal Auth

```perl theme={"dark"}
$client->auth->universal_auth->login(
    client_id     => '<machine-identity-client-id>',
    client_secret => '<machine-identity-client-secret>',
);
```

**Parameters:**

* `client_id` (string, optional): the client ID of the machine identity (defaults to the value of the `INFISICAL_UNIVERSAL_AUTH_CLIENT_ID` environment variable)
* `client_secret` (string, optional): the client secret of the machine identity (defaults to the value of the `INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET` environment variable)
* `organization_slug` (string, optional): the slug of a [sub-organization](https://infisical.com/docs/documentation/platform/sub-organizations.md) to log in to (defaults to the organization where the machine identity was created)

**Returns:**

* `Infisical::SDK`: the client, so you can create the client and log in with one statement

```perl theme={"dark"}
# Reads the client ID and client secret from the environment
my $client = Infisical::SDK->new->auth->universal_auth->login;
```

The SDK doesn't renew the access token that `login` gets. The token lasts as long as the **Access Token TTL** in the machine identity's Universal Auth settings. If your application runs longer than that, call `login` again.

### `secrets`

Each `secrets` method works on one folder, which these arguments select:

* `project_id` (string): the ID of your project
* `environment` (string): the slug of the environment, such as `dev`
* `secret_path` (string, optional): the path of the folder, such as `/backend` (defaults to `/`)

`list` and `get` replace each [secret reference](https://infisical.com/docs/documentation/platform/secret-reference.md#secret-referencing), such as `${dev.DB_HOST}`, with the value that the reference points to.

For the full reference of these methods, see [Infisical::SDK::Secrets](https://metacpan.org/pod/Infisical::SDK::Secrets).

#### List secrets

```perl theme={"dark"}
my @secrets = $client->secrets->list(
    project_id  => '<project-id>',
    environment => 'dev',
    secret_path => '/backend', # Optional
    recursive   => 1,          # Optional
);
```

**Parameters:**

* `recursive` (boolean, optional): whether to also list the secrets in every subfolder of `secret_path` (defaults to false)
* `view_secret_value` (boolean, optional): whether to return the secrets' values (defaults to true; if false, each secret's `secret_value_hidden` is true and its value is a placeholder)
* `tag_slugs` (array reference, optional): tag slugs, to list only the secrets that have at least one of these tags
* `attach_to_env` (boolean, optional): whether to also load the secrets into `%ENV` (defaults to false)

**Returns:**

* A list of `Infisical::SDK::Secret` objects, or the number of secrets if you call `list` in scalar context

#### Get secret by name

```perl theme={"dark"}
my $secret = $client->secrets->get(
    project_id  => '<project-id>',
    environment => 'dev',
    secret_path => '/backend', # Optional
    secret_name => 'DATABASE_URL',
    version     => 3,          # Optional
);

print $secret->secret_value;
```

**Parameters:**

* `secret_name` (string): the name of the secret
* `version` (integer, optional): the version of the secret to fetch (defaults to the latest version)
* `view_secret_value` (boolean, optional): whether to return the secret's value (defaults to true)

**Returns:**

* `Infisical::SDK::Secret`: the secret

#### Create secret

```perl theme={"dark"}
my $secret = $client->secrets->create(
    project_id      => '<project-id>',
    environment     => 'dev',
    secret_path     => '/backend',                                 # Optional
    secret_name     => 'STRIPE_KEY',
    secret_value    => 'sk_test_123',
    secret_comment  => 'Test mode key',                            # Optional
    tag_ids         => ['<tag-id>'],                               # Optional
    secret_metadata => [ { key => 'owner', value => 'billing' } ], # Optional
);
```

**Parameters:**

* `secret_name` (string): the name of the secret
* `secret_value` (string): the value of the secret, which can be an empty string
* `secret_comment` (string, optional): a comment that Infisical shows with the secret
* `tag_ids` (array reference, optional): the IDs of the tags to add to the secret
* `secret_metadata` (array reference, optional): key-value pairs to store with the secret, each a hash reference with `key`, `value`, and an optional boolean `is_encrypted`
* `skip_multiline_encoding` (boolean, optional): whether to store a multi-line value without encoding its line breaks

**Returns:**

* `Infisical::SDK::Secret`: the new secret, without its tags and metadata (call `get` for the full secret)

#### Update secret

```perl theme={"dark"}
my $secret = $client->secrets->update(
    project_id      => '<project-id>',
    environment     => 'dev',
    secret_path     => '/backend',          # Optional
    secret_name     => 'STRIPE_KEY',
    secret_value    => 'sk_test_456',       # Optional
    new_secret_name => 'STRIPE_SECRET_KEY', # Optional
);
```

**Parameters:**

* `secret_name` (string): the current name of the secret
* `new_secret_name` (string, optional): a new name for the secret
* `secret_value`, `secret_comment`, `tag_ids`, `secret_metadata`, and `skip_multiline_encoding` (optional): the new values, in the same format as for [`create`](#create-secret)

**Returns:**

* `Infisical::SDK::Secret`: the new version of the secret, in which `secret_value` and `secret_comment` hold only the values that this call sent, and tags and metadata are empty (call `get` for the full secret)

#### Delete secret

```perl theme={"dark"}
my $deleted = $client->secrets->delete(
    project_id  => '<project-id>',
    environment => 'dev',
    secret_path => '/backend', # Optional
    secret_name => 'STRIPE_SECRET_KEY',
);
```

**Parameters:**

* `secret_name` (string): the name of the secret

**Returns:**

* `Infisical::SDK::Secret`: the deleted secret, with the values it had before the delete

### Secret objects

`list`, `get`, `create`, `update`, and `delete` return `Infisical::SDK::Secret` objects. Each field has a read-only method of the same name:

| Method | Value |
| - | - |
| `secret_key` | The name of the secret |
| `secret_value` | The value of the secret |
| `secret_path` | The path of the folder that the secret is in |
| `environment` | The slug of the secret's environment |
| `version` | The version number of the secret |
| `secret_comment` | The secret's comment |
| `tags` | An array reference of the secret's tags |
| `secret_metadata` | An array reference of the secret's metadata, each a hash reference with `key`, `value`, and `is_encrypted` |
| `secret_value_hidden` | True if Infisical hid the value, because of `view_secret_value => 0` or because the machine identity can't read the value |

For an imported secret, `secret_path` and `environment` are the folder and environment that the secret was imported from. For every field, see [Infisical::SDK::Secret](https://metacpan.org/pod/Infisical::SDK::Secret).

## Error handling

If a method fails, it throws an `Infisical::SDK::Error` object, or an object of one of these subclasses:

| Class | When the SDK throws it |
| - | - |
| `Infisical::SDK::Error::Validation` | A method got a missing, unknown, or invalid argument, and the SDK sent nothing to Infisical |
| `Infisical::SDK::Error::Request` | Infisical returned an error (the object's `status_code`, `request_id`, and `body` methods return the details) |
| `Infisical::SDK::Error::Network` | The SDK got no response, for example because the connection failed or timed out |
| `Infisical::SDK::Error::ApprovalRequired` | The folder has an [approval policy](https://infisical.com/docs/documentation/platform/pr-workflows.md), so Infisical opened a change request instead of changing the secret (the object's `approval` method returns the change request) |

This example creates a secret if it doesn't exist:

```perl theme={"dark"}
use Scalar::Util qw(blessed);

my %args = (
    project_id  => '<project-id>',
    environment => 'dev',
    secret_name => 'SESSION_KEY',
);

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

    $secret = $client->secrets->create( %args, secret_value => '<new-session-key>' );
}
```

For every error class and method, see [Infisical::SDK::Error](https://metacpan.org/pod/Infisical::SDK::Error).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.