The Infisical .NET SDK provides a convenient way to interact with the Infisical API.
dotnet add package Infisical.Sdk
namespace Example;
using Infisical.Sdk;
using Infisical.Sdk.Model;
public class Program {
public static void Main(string[] args) {
var settings = new InfisicalSdkSettingsBuilder()
.WithHostUri("http://localhost:8080") // Optional. Will default to https://app.infisical.com
.Build();
var infisicalClient = new InfisicalClient(settings);
var _ = infisicalClient.Auth().UniversalAuth().LoginAsync("<machine-identity-universal-auth-client-id>", "<machine-identity-universal-auth-client-secret>").Result;
var options = new ListSecretsOptions
{
SetSecretsAsEnvironmentVariables = true,
EnvironmentSlug = "<your-env-slug>",
SecretPath = "/",
ProjectId = "<your-project-id>",
};
var secrets = infisicalClient.Secrets().ListAsync(options).Result;
if (secrets == null)
{
throw new Exception("Failed to fetch secrets, returned null response");
}
foreach (var secret in secrets)
{
Console.WriteLine($"{secret.SecretKey}: {secret.SecretValue}");
}
}
}
Imports Infisical.Sdk
Imports Infisical.Sdk.Model
Module Program
Sub Main(args As String())
Dim settings = New InfisicalSdkSettingsBuilder() _
.WithHostUri("https://app.infisical.com") _
.Build()
Dim infisicalClient As New InfisicalClient(settings)
Dim authResult = infisicalClient.Auth().UniversalAuth() _
.LoginAsync("<machine-identity-universal-auth-client-id>", "machine-identity-universal-auth-client-secret").Result
Dim options As New ListSecretsOptions With {
.SetSecretsAsEnvironmentVariables = True,
.EnvironmentSlug = "<your-env-slug>",
.SecretPath = "/",
.ProjectId = "<your-project-id>"
}
Dim secrets = infisicalClient.Secrets().ListAsync(options).Result
For Each secret In secrets
Console.WriteLine(secret.SecretKey)
if Environment.GetEnvironmentVariable(secret.SecretKey) IsNot Nothing Then
Console.WriteLine("{0} found on environment variables", secret.SecretKey)
End If
Next
End Sub
End Module
The SDK methods are organized into the following high-level categories:
Auth()
: Handles authentication methods.Secrets()
: Manages CRUD operations for secrets.
The Auth
component provides methods for authentication:
var _ = await sdk.Auth().UniversalAuth().LoginAsync(
"CLIENT_ID",
"CLIENT_SECRET"
);
Parameters:
clientId
(string): The client ID of your Machine Identity.clientSecret
(string): The client secret of your Machine Identity.
This sub-class handles operations related to secrets:
Task<Secret[]> ListAsync(ListSecretsOptions options);
throws InfisicalException
var options = new ListSecretsOptions
{
SetSecretsAsEnvironmentVariables = true,
EnvironmentSlug = "dev",
SecretPath = "/test",
Recursive = true,
ExpandSecretReferences = true,
ProjectId = projectId,
ViewSecretValue = true,
};
Secret[] secrets = await sdk.Secrets().ListAsync(options);
ListSecretsOptions:
ProjectId
(string): The ID of your project.EnvironmentSlug
(string): The environment in which to list secrets (e.g., "dev").SecretPath
(string): The path to the secrets.ExpandSecretReferences
(boolean): Whether to expand secret references.Recursive
(boolean): Whether to list secrets recursively.SetSecretsAsEnvironmentVariables
(boolean): Set the retrieved secrets as environment variables.
Returns:
Task<Secret[]>
: The response containing the list of secrets.
public Task<Secret> CreateAsync(CreateSecretOptions options);
throws InfisicalException
var options = new CreateSecretOptions
{
SecretName = "SECRET_NAME",
SecretValue = "SECRET_VALUE",
EnvironmentSlug = "<environment-slug>",
SecretPath = "/",
ProjectId = "<your-project-id>",
Metadata = new SecretMetadata[] {
new SecretMetadata {
Key = "metadata-key",
Value = "metadata-value"
}
}
};
Task<Secret> newSecret = await sdk.Secrets().CreateAsync(options);
Parameters:
SecretName
(string): The name of the secret to createSecretValue
(string): The value of the secret.ProjectId
(string): The ID of your project.EnvironmentSlug
(string): The environment in which to create the secret.SecretPath
(string, optional): The path to the secret.Metadata
(object, optional): Attach metadata to the secret.SecretComment
(string, optional): Attach a secret comment to the secret.SecretReminderNote
(string, optional): Attach a secret reminder note to the secret.SecretReminderRepeatDays
(int, optional): Set the reminder repeat days on the secret.SkipMultilineEncoding
(bool, optional): Weather or not to skip multiline encoding for the secret's value. Defaults tofalse
.
Returns:
Task<Secret>
: The created secret.
public Task<Secret> UpdateAsync(UpdateSecretOptions options);
throws InfisicalException
var updateSecretOptions = new UpdateSecretOptions
{
SecretName = "EXISTING_SECRET_NAME",
EnvironmentSlug = "<environment-slug>",
SecretPath = "/",
NewSecretName = "NEW_SECRET_NAME",
NewSecretValue = "new-secret-value",
ProjectId = "<project-id>",
};
Task<Secret> updatedSecret = await sdk.Secrets().UpdateAsync(options);
Parameters:
SecretName
(string): The name of the secret to update.`ProjectId
(string): The ID of your project.EnvironmentSlug
(string): The environment in which to update the secret.SecretPath
(string): The path to the secret.NewSecretValue
(string, optional): The new value of the secret.NewSecretName
(string, optional): A new name for the secret.NewMetadata
(object, optional): New metadata to attach to the secret.
Returns:
Task<Secret>
: The updated secret.
public Secret GetAsync(GetSecretOptions options);
throws InfisicalException
var getSecretOptions = new GetSecretOptions
{
SecretName = "SECRET_NAME",
EnvironmentSlug = "<environment-slug>",
SecretPath = "/",
ProjectId = "<project-id>",
};
Secret secret = await sdk.Secrets().GetAsync(options);
Parameters:
SecretName
(string): The name of the secret to get`ProjectId
(string): The ID of your project.EnvironmentSlug
(string): The environment in which to retrieve the secret.SecretPath
(string): The path to the secret.ExpandSecretReferences
(boolean, optional): Whether to expand secret references.Type
(SecretType, optional): The type of secret to fetch. Defaults toShared
.
Returns:
Task<Secret>
: The fetched secret.
public Secret DeleteAsync(DeleteSecretOptions options);
throws InfisicalException
var options = new DeleteSecretOptions
{
SecretName = "SECRET_TO_DELETE",
EnvironmentSlug = "<environment-slug>",
SecretPath = "/",
ProjectId = "<project-id>",
};
Secret deletedSecret = await sdk.Secrets().DeleteAsync(options);
Parameters:
SecretName
(string): The name of the secret to delete.ProjectId
(string): The ID of your project.EnvironmentSlug
(string): The environment in which to delete the secret.SecretPath
(string, optional): The path to the secret.
Returns:
Task<Secret>
: The deleted secret.