Skip to main content
You can use the Infisical API to create a new session every time your agent starts. Your code authenticates as a machine identity, creates a session with an access bundle, passes the session token to the agent, and revokes the session when the agent finishes. This is useful if your code starts your agents or creates sessions for agents on other machines, such as a job runner, an orchestrator, or a backend that runs an agent for each task.
If a shell command starts the agent on the machine where it runs, such as a cron job or a container entrypoint, the Infisical CLI can create and revoke each session for you without any code.

Prerequisites

  • An access bundle with a service for each API the agent calls
  • A running proxy that the agent’s machine can reach
  • The Admin role in Agent Vault, or an Agent Vault admin who can complete Step 1 for you

Step 1: Set up a machine identity

Your code creates sessions as a machine identity, so that sessions don’t depend on a person’s account.
1
In Agent Vault, go to Access Control > Machine Identities and select Add Machine Identity.
2
On the Create New tab, enter a Name, choose Member in Role, then select Create. Infisical creates the machine identity with Universal Auth and opens its page.
To use a machine identity that already exists in your organization, select the Assign Existing tab instead.
3
On the machine identity’s page, select Universal Auth under Authentication. Copy the Client ID, then select Add Client Secret and copy the client secret.
4
Go to Access Bundles and open the access bundle the agent uses. Select Options > Manage Access, then select Grant Access. Select the machine identity, then select Grant Access.

Step 2: Log in as the machine identity

Call the login endpoint for Universal Auth with the client ID and client secret:
If you self-host Infisical, replace https://app.infisical.com with the address of your instance, here and in every later request. The response includes an accessToken, which you send with each request in the next steps, and expiresIn, the number of seconds until the access token expires. Log in again when the access token expires.
If your code runs on a cloud provider or in Kubernetes, you can use another authentication method, such as AWS Auth or Kubernetes Auth, so that your code doesn’t need a client secret. Each method returns an access token that works the same way in the next steps.

Step 3: Create a session

Call the endpoint that creates a session with the name of the access bundle and how long the session should last:
  • accessBundles takes a list, but a session can have only one access bundle, so the list must have exactly one name
  • ttl accepts a duration of at least one minute, such as 30m, 8h, or 7d, or never, and defaults to 7d (see Choose how long a session lasts to pick a value)
The response includes the session’s ID, the session token, and when the session expires:
Save the ID, which you need to revoke the session in Step 5. You can’t retrieve the session token again after this response.

Choose how long a session lasts

Pick the ttl based on how long your agents run:

Step 4: Start the agent with the session token

The agent uses the session token to authenticate to the proxy. How you pass the token to the agent depends on how you start the agent:
Start the agent with infisical agent-vault run, and pass the session token and the proxy’s address:
The CLI trusts the proxy’s certificate for the agent and sends the agent’s requests through the proxy.

Step 5: Revoke the session

When the agent finishes, call the endpoint that revokes a session with the session’s ID:
The proxy stops attaching credentials for the session within one poll interval. Revoke the session even if the agent fails, for example in a finally block or an exit handler. If your code exits before it can revoke the session, the session stops working when its ttl runs out.

Step 6: Verify it works

In Agent Vault, go to Sessions and select All Sessions. The Identity column shows the machine identity’s name for the session you created. The session’s Status is Active while the agent runs, and Revoked after Step 5. If you’ve set up session logs, view the session’s logs to check which requests the agent made during the session.
Your code now creates a session for each agent run, and the agent’s credentials stop working when you revoke the session or its ttl runs out.

Troubleshooting

The machine identity doesn’t have a grant on the access bundle, or the name in accessBundles is wrong. Check the name on the Access Bundles page, and grant the bundle to the machine identity.
The machine identity’s access token expired. Log in again to get a new access token. Sessions you already created keep working, because a session doesn’t depend on the access token that created it.

Next steps

Your own agent

Send your agent’s requests through the proxy from your own code.

Session logs

Record every request an agent makes during a session.