Tutorial: Secure AI agent access with 1Password SDKs
In this tutorial, you'll learn a secure workflow for providing sensitive credentials stored in 1Password to an AI agent using 1Password SDKs.
We'll walk through the process using an example integration with Anthropic Claude that automatically books a flight with your company credit card then submits an expense report in Ramp, all without hardcoding any secrets. By the end, you'll understand how to:
- Follow the principle of least privilege to make sure your AI agent only has the minimum access needed to perform your task.
- Create a 1Password Service Account with least privilege access to relevant items in your 1Password account.
- Create secret reference URIs that point to where your credentials are stored in 1Password, so you can avoid hardcoding your secrets in plaintext.
- Use the 1Password SDKs to securely fetch the secrets your AI agent needs at runtime.
With this workflow, your AI agent can securely access secrets in 1Password to authenticate into services. And you can see what items the agent accesses by creating a service account usage report.
Prerequisites
- 1Password subscription.
- 1Password desktop app. (Optional)
- Basic knowledge of AI agents.
- Basic knowledge of Python.
- Familiarity with 1Password SDKs. To learn how to get started, see the end-to-end setup tutorial.
Part 1: Set up a 1Password Service Account scoped to a vault
In the first part of this tutorial, you'll learn how to use 1Password to follow the security principle of least privilege, which requires that a process only be given the minimum level of access needed to complete its task.
To do this, you'll create a vault in your 1Password account that only contains the secrets your AI agent needs. Then you'll create a service account that only has read access to the new vault, and can't access any other items in your account. When your agent authenticates to 1Password using the service account, it won't have any unnecessary access or permissions beyond the bare minimum.
Step 1: Create a vault that only contains items required for the task
First, create a vault that only contains the credentials you'll need to perform the task you want the AI agent to complete. For our example, we'll create a new vault Tutorial
that contains our Navan and Ramp logins, and our travel credit card.
- Open and unlock the 1Password app.
- Select the plus button in the sidebar next to your account name.
- Enter
Tutorial
for the vault name, then select Create. - Move or copy the items you need for the task into the vault.
Step 2: Create a service account scoped to the vault
Service accounts are a token-based authentication method that you can scope to specific vaults and permissions. For this tutorial, we'll create a service account that only has read access in the Tutorial
vault.
If you don't see the option to create service accounts, ask your administrator to give you access to create and manage service accounts.
- Sign in to your account on 1Password.com.
- Select Developer in the sidebar. Or, if you already have active applications and services, select Directory at the top of the Developer page.
- Under Access Tokens, select Service Account.
- Give your service account a name. For example,
AI Agent Workflow Service Account
, then select Next. - On the next screen, you'll see a list of your 1Password vaults. Select the Tutorial vault you created in the previous step, then select the gear icon next to it. In the permissions dropdown, select Read Items.
- Select Create Account.
- On the next screen, select Save in 1Password, then save your newly-created service account token in the Tutorial vault.
Part 2: Securely provide your credentials to the agent
In the second part of this tutorial, you'll learn how to build an AI agent integration that securely fetches your credentials from 1Password at runtime.
To do this, you'll use the secrets.resolve()
method with secret reference URIs that point to where your credentials are stored in your 1Password account. When the agent runs, 1Password injects the actual secrets referenced by the URIs.
This setup makes sure that your agent can only work with the credentials you explicitly provide as secret references in your non-dynamic controller code. This creates a clear boundary between your 1Password account and the AI agent, and prevents the agent from crafting its own requests to 1Password or accessing other credentials.
Set up a project for your AI agent integration using 1Password SDKs. In the example below, we've created an integration using the Python SDK. Learn how to get started with 1Password SDKs.
Step 1: Export your service account token
Export the service account token you saved in part one to the OP_SERVICE_ACCOUNT_TOKEN
environment variable.
- Bash, Zsh, sh
- fish
- PowerShell
Step 2: Define your credentials
Define the credentials your AI agent will need using the secrets.resolve()
method from the 1Password SDK. You can use placeholder secret references for now – we'll replace them with real secret references in the next step.
In our example, we've defined:
- Our Navan username and password.
- Our travel credit card number, expiration date, and CVC.
- Our Ramp username and password.
Step 3: Get secret references
Get secret reference URIs for your credentials, then paste them into your script in place of the placeholders from the previous step.
- Open and unlock the 1Password desktop app.
- Turn on the integration with 1Password CLI.
- Open the Tutorial vault and select an item that contains a credential you want to reference in your script.
- Select the down arrow next to the field for the secret you want to reference, then select Copy Secret Reference.
- Paste the secret reference into your code in place of
op://vault/item/field
.
You can also create secret references using the 1Password for VS Code extension.
Here's our example updated with secret references:
Step 4: Define your agent instructions
Now, provide the AI agent instructions for how to use the credentials you fetched in the previous step. In our example, we instruct the agent to book a flight using our company credit card, then file an expense report for reimbursement.
AI agents can make mistakes. Make sure to double check the results of your prompts.
Run the script, and the agent will securely load your secrets from 1Password and perform the defined tasks.
Conclusion
In this tutorial, you learned how to securely provide an AI agent with access credentials to perform a specific task, without hardcoding any secrets or giving the agent unnecessary access permissions.
You can modify the provided example to work with other AI agents or language models, and extend it to support a wide range of tasks.