Skip to main content
If you’re working with Go, the official Infisical Go SDK package is the easiest way to fetch and work with secrets for your application.

Basic usage

This example demonstrates how to use the Infisical Go SDK in a simple Go application. The application retrieves a secret named API_KEY from the dev environment of the YOUR_PROJECT_ID project.
We don’t recommend hardcoding your Machine Identity Tokens. Setting it as an environment variable would be best.

Installation

Configuration

Import the SDK and create a client instance.

Configuration options

object

Automatic token refreshing

The Infisical Go SDK supports automatic token refreshing, enabled by default. After using one of the auth methods such as Universal Auth, the SDK will automatically renew and re-authenticate when needed. You can opt-out by setting AutoTokenRefresh to false in the client settings.

Client lifecycle

When using automatic token refreshing it’s important to understand how your application uses the Infisical client. If you’re instantiating new instances of the client often, it’s important to cancel the context when the client is no longer needed to avoid the token refreshing process from running indefinitely.
This is only necessary if you’re creating multiple instances of the client, and those instances are deleted or otherwise removed throughout the application lifecycle. If you’re only creating one instance of the client, and it will be used throughout the lifetime of your application, you don’t need to worry about this.

Authentication

Authentication methods

The SDK supports a variety of authentication methods. The most common authentication method is Universal Auth, which uses a client ID and client secret to authenticate.
Using environment variablesCall .Auth().UniversalAuthLogin() with empty arguments to use the following environment variables:
  • INFISICAL_UNIVERSAL_AUTH_CLIENT_ID - Your machine identity client ID.
  • INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET - Your machine identity client secret.
Using the SDK directly
Please note that this authentication method will only work if you’re running your application on Google Cloud Platform. Please read more about this authentication method.
Using environment variablesCall .Auth().GcpIdTokenAuthLogin() with empty arguments to use the following environment variables:
  • INFISICAL_GCP_AUTH_IDENTITY_ID - Your Infisical Machine Identity ID.
Using the SDK directly
Using environment variablesCall .Auth().GcpIamAuthLogin() with empty arguments to use the following environment variables:
  • INFISICAL_GCP_IAM_AUTH_IDENTITY_ID - Your Infisical Machine Identity ID.
  • INFISICAL_GCP_IAM_SERVICE_ACCOUNT_KEY_FILE_PATH - The path to your GCP service account key file.
Using the SDK directly
Please note that this authentication method will only work if you’re running your application on AWS. Please read more about this authentication method.
Using environment variablesCall .Auth().AwsIamAuthLogin() with empty arguments to use the following environment variables:
  • INFISICAL_AWS_IAM_AUTH_IDENTITY_ID - Your Infisical Machine Identity ID.
Using the SDK directly
Please note that this authentication method will only work if you’re running your application on Azure. Please read more about this authentication method.
Using environment variablesCall .Auth().AzureAuthLogin() with empty arguments to use the following environment variables:
  • INFISICAL_AZURE_AUTH_IDENTITY_ID - Your Infisical Machine Identity ID.
Using the SDK directly
Please note that this authentication method will only work if you’re running your application on Kubernetes. Please read more about this authentication method.
Using environment variablesCall .Auth().KubernetesAuthLogin() with empty arguments to use the following environment variables:
  • INFISICAL_KUBERNETES_IDENTITY_ID - Your Infisical Machine Identity ID.
  • INFISICAL_KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH_ENV_NAME - The environment variable name that contains the path to the service account token. This is optional and will default to /var/run/secrets/kubernetes.io/serviceaccount/token.
Using the SDK directly
Please note that this authentication method requires a valid JWT token from your JWT issuer. Please read more about this authentication method.
Using the SDK
Please note that this authentication method requires LDAP credentials. Please read more about this authentication method.
Using environment variablesYou can set the INFISICAL_LDAP_AUTH_IDENTITY_ID environment variable and pass empty string for the identity ID:
Using the SDK directly
Please note that this authentication method will only work if you’re running your application on Oracle Cloud Infrastructure. Please read more about this authentication method.
Using environment variablesYou can set the INFISICAL_OCI_AUTH_IDENTITY_ID environment variable and omit the IdentityID field:
Using the SDK directly
OciAuthLoginOptions fields:
  • IdentityID (string) - Your Infisical Machine Identity ID. Can be set via INFISICAL_OCI_AUTH_IDENTITY_ID environment variable.
  • UserID (string) - Your OCI user OCID.
  • TenancyID (string) - Your OCI tenancy OCID.
  • Fingerprint (string) - Your OCI API key fingerprint.
  • PrivateKey (string) - Your OCI private key (PEM format).
  • Region (string) - Your OCI region (e.g., us-ashburn-1).
  • Passphrase (*string) - Optional: pointer to passphrase string if your private key is encrypted.

Sub-organization authentication

This section is only relevant to you if your organization is utilizing sub-organizations within Infisical.
All SDK authentication methods support logging into a sub-organization that your machine identity has access to. This is optional and only necessary when attempting to authenticate into a sub-organization using an identity created at the root organization. Use the .Auth().WithOrganizationSlug("<organization-slug>") method to specify which organization to authenticate against:
If no organization slug is provided, the authentication session defaults to the organization where the machine identity was originally created.

Secrets

List secrets

client.Secrets().List(options) Retrieve all secrets within the Infisical project and environment that client is connected to.

Parameters

object

Retrieve secret

client.Secrets().Retrieve(options) Retrieve a secret from Infisical. By default Secrets().Retrieve() fetches and returns a shared secret.

Parameters

object

Create secret

client.Secrets().Create(options) Create a new secret in Infisical.

Parameters

object

Update secret

client.Secrets().Update(options) Update an existing secret in Infisical.

Parameters

object

Delete secret

client.Secrets().Delete(options) Delete a secret in Infisical.

Parameters

object

Batch create secrets

client.Secrets().Batch().Create(options) Create multiple secrets in Infisical.

Parameters

object

Folders

List folders

client.Folders().List(options) Retrieve all within the Infisical project and environment that client is connected to.

Parameters

object

Create folder

client.Folders().Create(options) Create a new folder in Infisical.

Parameters

object

Update folder

client.Folders().Update(options) Update an existing folder in Infisical.

Parameters

object

Delete folder

client.Folders().Delete(options) Delete a folder in Infisical.

Parameters

object

Dynamic secrets

Dynamic secrets generate credentials on-demand instead of storing static values. Credentials are issued through short-lived leases that Infisical automatically revokes when they expire.
The Go SDK reads dynamic secret configurations and manages their leases. Creating or updating a dynamic secret’s configuration is done through the Infisical dashboard or the API.

List dynamic secrets

client.DynamicSecrets().List(options) Retrieve all dynamic secrets within a project environment and path.

Parameters

object

Get dynamic secret by name

client.DynamicSecrets().GetByName(options) Retrieve a single dynamic secret configuration by name.

Parameters

object

Dynamic secret leases

A lease represents a single set of generated credentials with its own TTL. Use leases to generate, inspect, renew, and revoke credentials from a dynamic secret. Access the leases interface with client.DynamicSecrets().Leases().

Create lease

client.DynamicSecrets().Leases().Create(options) Generate a new set of credentials from a dynamic secret.
Your generated credentials are returned in the first return value (credentials, a map[string]any). The exact keys depend on the dynamic secret provider.

Parameters

object

Return

(map[string]any, DynamicSecret, DynamicSecretLease, error)
The generated credentials, the dynamic secret the lease was created from, the lease details, and an error if the request failed.

List leases

client.DynamicSecrets().Leases().List(options) Retrieve all leases for a dynamic secret.

Parameters

object

Get lease by ID

client.DynamicSecrets().Leases().GetById(options) Retrieve a single lease, including its associated dynamic secret.

Parameters

object

Return (map)

(DynamicSecretLeaseWithDynamicSecret, error)
The lease, with the associated dynamic secret nested in its DynamicSecret field, and an error if the request failed.

Renew lease

client.DynamicSecrets().Leases().RenewById(options) Extend the TTL of an existing lease.
Renewals must happen before the lease expires. Renewing doesn’t issue new credentials; it extends the expiration of the existing credentials.

Parameters

object

Delete lease

client.DynamicSecrets().Leases().DeleteById(options) Revoke a lease and its associated credentials.

Parameters

object

KMS

Create key

client.Kms().Keys().Create(options) Create a new key in Infisical.

Parameters

object

Return (object)

object

Delete key

client.Kms().Keys().Delete(options) Delete a key in Infisical.

Parameters

object

Return

object

Signing data

client.Kms().Signing().Sign(options) Sign data in Infisical.

Parameters

object

Return ([]byte)

[]byte
The signature of the data that was signed.

Verifying data

client.Kms().Signing().Verify(options) Verify data in Infisical.

Parameters

object

Return (object)

object

List signing algorithms

client.Kms().Signing().ListSigningAlgorithms(options) List signing algorithms in Infisical.

Parameters

object

Return ([]string)

[]string
The signing algorithms that are available for the key.

Get public key

This method is only available for keys with key usage sign-verify. If you attempt to use this method on a key that is intended for encryption/decryption, it will return an error.
client.Kms().Signing().GetPublicKey(options) Get the public key in Infisical.

Parameters

object

Return (string)

string
The public key for the key.

Encrypt data

client.Kms().Encryption().Encrypt(options) Encrypt data with a key in Infisical KMS.

Parameters

object

Return (string)

string
The encrypted data.

Decrypt data

client.Kms().DecryptData(options) Decrypt data with a key in Infisical KMS.

Parameters

object

Return (string)

string
The decrypted data.

Agent Vault sessions

client.AgentVault().Sessions() manages Agent Vault sessions. A session lets an AI agent use the services in one access bundle, the inline services sent with the session, or both, until the session expires or someone revokes it. What the client can do with sessions depends on the role that the user or machine identity the client logs in as has in the Agent Vault project:
AgentVault() requires github.com/infisical/go-sdk version 0.10.0 or newer.

List sessions

Parameters:
AgentVaultListSessionsOptions
required
Returns:
AgentVaultListSessionsResult

Get session

Parameters:
string
required
The ID of the session.
Returns:
AgentVaultSession
The session.

Create session

Parameters:
AgentVaultCreateSessionOptions
required
Returns:
AgentVaultCreatedSession
The new session, with the session token in Token. Infisical returns the token only once.
To pass session.Token to the agent, follow the steps in Start the agent.

Create a session on behalf of a user

If the client logs in as a machine identity with a delegation, the client can create sessions on behalf of the users that the delegation covers. For each of those sessions, the proxy uses the user’s personal values instead of the machine identity’s.
  • The session can use an access bundle only if both the user and the machine identity can use the bundle
  • The session belongs to the machine identity, so the user can’t see the session unless they’re an Agent Vault admin
  • In each session that the Get, List, Update, and Revoke methods return, OnBehalfOf is the user, or nil if the session is the machine identity’s own
To find the access bundles that a session on behalf of the user can use, set OnBehalfOf in AccessBundles().List. The proxy refuses a session’s requests to services that still need the user’s personal values, so also set RequiresSetup to infisical.BoolPtr(false) to leave out the access bundles with those services.

Create a session with inline services

If the client logs in as a user or machine identity with the Admin role in the Agent Vault project, the client can send inline services with a session, instead of or beside an access bundle. Inline services let you pass along credentials that your code already holds, without setting anything up in Agent Vault in advance.
  • Each service is an HTTP API (AgentVaultInlineHTTPService), an MCP server (AgentVaultInlineMCPService), or a database or SSH server (AgentVaultInlineDatabaseService or AgentVaultInlineSSHService)
  • Credentials, custom header values, and substitution values can’t contain text in double braces, such as {{GITHUB_TOKEN}}, because inline services can’t use variables
  • A session can have up to 30 inline services, and no two of its services can have the same name or host pattern, including the services in its access bundle
  • If a machine identity creates the session on behalf of a user, the machine identity needs the Admin role and the user doesn’t, and the inline services don’t use the user’s personal values
  • In each session that the Create, Get, List, and Update methods return, Services lists the inline services without their secrets
For the fields that each type of service takes, and for when Infisical deletes the services’ secrets, see Inline services.

Update session label and metadata

Add, change, or remove a session’s label or metadata, for example to record what the agent is working on. You can change them after the session expires or is revoked, unless someone deleted the session’s owner or the user that the session was created on behalf of.
Parameters:
string
required
The ID of the session.
AgentVaultUpdateSessionOptions
required
Returns:
AgentVaultSession
The updated session.

Revoke session

Revoke a session when its agent finishes, even if the agent fails. The proxy stops attaching credentials to the session’s requests within one poll interval. Set TTL close to how long the agent runs, so that the session expires soon after the agent finishes even if revoking the session fails.
Parameters:
string
required
The ID of the session.
Returns:
AgentVaultSession
The revoked session, with AccessBundle and Services set to nil. To get them, call Get.

Agent Vault proxies

client.AgentVault().Proxies() manages Agent Vault proxies. A proxy attaches API credentials to the requests that agents send through it. To use any method in this section except List, the client must log in as a user or machine identity with the Admin role in the Agent Vault project.

List proxies

Parameters:
AgentVaultListProxiesOptions
required
Returns:
AgentVaultListProxiesResult
If the client logs in as a member, the proxies have no TrafficPolicy, AllowedHosts, PollInterval, or CreatedAt.

Create proxy

Parameters:
AgentVaultCreateProxyOptions
required
Returns:
AgentVaultProxyWithToken
To start the proxy with the proxy token, run one of the commands in Create a proxy and start it.

Update proxy

The proxy’s name, description, and settings that you don’t set stay as they are. If the proxy is running, it applies the changes within one poll interval.
Parameters:
string
required
The ID of the proxy.
AgentVaultUpdateProxyOptions
required
Returns:
AgentVaultProxy
The updated proxy.

Delete proxy

You can’t undo deleting a proxy. To stop a proxy temporarily, revoke its access instead.
Parameters:
string
required
The ID of the proxy.
Returns:
AgentVaultProxyRef
The deleted proxy’s ID and Name.

Rotate proxy token

Issue a new proxy token if the current token leaked. Rotating the token also restores a revoked proxy’s access. The old token stops working right away. To restart the proxy and its agents, follow the steps in Rotate a proxy’s token.
Parameters:
string
required
The ID of the proxy.
Returns:
AgentVaultProxyWithToken

Revoke proxy

Revoke a proxy’s access if you think a machine that runs the proxy is compromised. The proxy stops attaching credentials to requests until you rotate its token.
Parameters:
string
required
The ID of the proxy.
Returns:
AgentVaultProxy
The revoked proxy.

Agent Vault access bundles

client.AgentVault().AccessBundles() manages Agent Vault access bundles and who they’re granted to. An access bundle is a list of services that an agent can use during a session. To use any method in this section except List and Get, the client must log in as a user or machine identity with the Admin role in the Agent Vault project.

List access bundles

Parameters:
AgentVaultListAccessBundlesOptions
required
Returns:
AgentVaultListAccessBundlesResult
If the client logs in as a member, List returns only the access bundles granted to the member. If you set OnBehalfOf, the client must log in as a machine identity with a delegation that covers the user. List then returns only the access bundles that both the user and the machine identity can use, and RequiresSetup and ServicesRequiringSetup refer to the user’s personal values. You can’t combine OnBehalfOf with Granted.

Get access bundle

Parameters:
string
required
The ID of the access bundle.
Returns:
AgentVaultAccessBundleWithServices
The access bundle, with its services in Services. Each service has only the fields of its Type, so check Type before you read a field such as HostPattern.

Create access bundle

Parameters:
AgentVaultCreateAccessBundleOptions
required
Returns:
AgentVaultAccessBundle
The new access bundle.
Infisical grants the new access bundle to you, unless you’re in the Agent Vault project only through a group. The SDK can’t add services to an access bundle. To add them in Infisical, see Services.

Update access bundle

The values that you don’t set stay as they are.
Parameters:
string
required
The ID of the access bundle.
AgentVaultUpdateAccessBundleOptions
required
Returns:
AgentVaultAccessBundle
The updated access bundle.

Delete access bundle

When you delete an access bundle, the sessions that use it stop getting credentials for the bundle’s services (see Access changes). A session’s inline services keep working, so to stop every credential that a session gets, revoke the session.
Parameters:
string
required
The ID of the access bundle.
Returns:
AgentVaultAccessBundle
The deleted access bundle.

List grantees

List the users, machine identities, and groups that an access bundle is granted to.
Parameters:
string
required
The ID of the access bundle.
AgentVaultListGranteesOptions
required
Returns:
AgentVaultListGranteesResult

List available grantees

List the members of the Agent Vault project that an access bundle isn’t granted to yet.
Parameters:
string
required
The ID of the access bundle.
AgentVaultListGranteesOptions
required
Returns:
AgentVaultListMembersResult

Grant access

Grant an access bundle to users, machine identities, or groups. Pass at least one ID, and at most 100 in total. Each user, machine identity, and group must be a member of the Agent Vault project. If a user or machine identity is in the Agent Vault project only through a group, Grant fails and grants nothing. Grant the access bundle to the group instead, or first add the user or machine identity to the Agent Vault project.
Parameters:
string
required
The ID of the access bundle.
AgentVaultChangeGrantsOptions
required
Returns:
AgentVaultGrantResult

Revoke access

Revoke an access bundle from users, machine identities, or groups. Pass at least one ID, and at most 100 in total.
  • If someone with the Member role no longer has any grant on the access bundle, the proxy stops attaching the bundle’s credentials to their sessions and to the sessions that machine identities created on their behalf (see Access changes; a session’s inline services keep working, so to stop every credential, revoke the session)
  • Admins can use every access bundle without a grant, so their sessions keep getting credentials after you revoke their grants (to stop one of their agents, revoke its session)
Parameters:
string
required
The ID of the access bundle.
AgentVaultChangeGrantsOptions
required
Returns:
AgentVaultRevokeGrantsResult

Agent Vault delegations

client.AgentVault().Delegations() manages delegations, which let a machine identity create sessions on behalf of users. To use any method in this section, the client must log in as a user or machine identity with the Admin role in the Agent Vault project. A machine identity without the role can still get its own delegation.

List delegations

Parameters:
AgentVaultListDelegationsOptions
required
Returns:
AgentVaultListDelegationsResult

Get delegation

Get which users a machine identity can create sessions on behalf of.
Parameters:
string
required
The ID of the machine identity.
AgentVaultProjectOptions
required
Returns:
AgentVaultDelegation
The delegation, including MissingAccessBundles.
If the machine identity has no delegation, Get returns an *errors.APIError whose StatusCode is 404. The errors package is github.com/infisical/go-sdk/packages/errors. MissingAccessBundles lists the access bundles that the delegation’s users can use and the machine identity can’t. A session on behalf of a user can use only an access bundle that both the user and the machine identity can use, so the machine identity can’t create sessions with those access bundles. If a machine identity without the Admin role gets its own delegation, MissingAccessBundles is empty.

Set delegation

Set which users a machine identity can create sessions on behalf of. The new delegation replaces any delegation that the machine identity has.
Parameters:
string
required
The ID of the machine identity, which must be a member of the Agent Vault project.
AgentVaultSetDelegationOptions
required
Returns:
AgentVaultDelegation
The delegation, with MissingAccessBundles.
With AgentVaultDelegationScopeSelected, pass at least one user or group. With AgentVaultDelegationScopeAllMembers, pass no users or groups. To stop the machine identity from acting on behalf of anyone, delete the delegation. Each user and group must be a member of the Agent Vault project. If a user is in the Agent Vault project only through a group, pass the group instead.

List available users and groups

List the users and groups in the Agent Vault project that you can add to a machine identity’s delegation.
Parameters:
string
required
The ID of the machine identity.
AgentVaultListAvailableDelegationSubjectsOptions
required
Returns:
AgentVaultListMembersResult

Add users and groups

Add users and groups to a machine identity’s delegation. The users and groups that are already in the delegation stay in it. Pass at least one ID.
  • If the machine identity has no delegation, AddSubjects creates one with the AgentVaultDelegationScopeSelected scope
  • If the delegation covers all members, you can’t add users or groups to it
Parameters:
string
required
The ID of the machine identity.
AgentVaultChangeDelegationSubjectsOptions
required
Returns:
AgentVaultDelegation
The delegation after the change, with MissingAccessBundles.

Remove users and groups

Remove users and groups from a machine identity’s delegation. Pass at least one ID.
  • If the delegation no longer covers a user, the proxy refuses the sessions that the machine identity created on behalf of the user until the delegation covers the user again (to end those sessions for good, revoke them)
  • If you remove the last user or group, Infisical deletes the delegation
Parameters:
string
required
The ID of the machine identity.
AgentVaultChangeDelegationSubjectsOptions
required
Returns:
*AgentVaultDelegation
The delegation after the change, with MissingAccessBundles, or nil if Infisical deleted the delegation.

Delete delegation

Stop a machine identity from creating sessions on behalf of users. The proxy refuses the sessions that the machine identity already created on behalf of users until a delegation covers those users again. To end those sessions for good, revoke them.
Parameters:
string
required
The ID of the machine identity.
AgentVaultProjectOptions
required
Returns:
AgentVaultDelegation
The deleted delegation, without MissingAccessBundles.