Content

How to Connect Bitbucket to an AI Agent (2026)

Bitbucket's app passwords and API tokens authenticate with different usernames, and mixing them up is what causes a silent 401. Here's how Swytchcode handles the credential pairing and gates write access behind policy.

EngineeringOct 30, 2026

Key takeaways

  • -App passwords and API tokens authenticate with different usernames: an app password uses the Bitbucket username, an API token uses the Atlassian account email. Mixing them up is what causes a 401.
  • -Atlassian stopped creating new app passwords on September 9, 2025 and disabled existing ones on June 9, 2026. API tokens are now the only Basic-auth option.
  • -Scope a token to one workspace and the narrowest permission it needs. A token minted with no scopes won't call the API at all.
  • -Swytchcode keeps the email/token pair in its own local store and lets policy deny write methods like push and merge until a task genuinely needs them.

Switching an agent from a Bitbucket app password to an API token looks like a one-line change. It isn't, because the two authenticate as different identities, and an agent that mixes them up won't fail at setup. It'll fail days later, on a call that used to work.

Tuesday's agent could list commits just fine. Wednesday it gets a 401 on the same repository. The token in the store is new, but the request still authenticates with the Bitbucket username, because that's what the old app-password snippet did. API tokens don't want that username at all. They want the Atlassian account email. The 401 is really an identity mismatch, and the repository never even saw a valid call.

That's the problem with Bitbucket credentials for an agent: app passwords and API tokens don't just swap in for each other. They authenticate with a completely different username, and code upgraded to use a token while still sending the old username fails without explaining why. Swytchcode's Bitbucket integration handles that pairing for you, so swapping credentials doesn't quietly break whatever used to work.

The problem: Bitbucket's two credential types don't share a username

Atlassian's own note on 401 errors draws the line clearly: an app password pairs with your Bitbucket username, while an API token pairs with your Atlassian account email instead. Neither credential recognizes the other one's username, and a token minted with no permissions attached simply won't call the API at all. That looks identical to a permissions bug, but it's really a setup mistake.

There's a second risk layered on top: a token can be scoped narrowly to one workspace and to only the permissions an agent actually needs, or it can be minted wide open. Widening a token just to make an error go away is exactly how a review bot ends up with write access across an entire workspace, instead of read access on the one repository it was supposed to look at.

How Swytchcode solves it

Swytchcode downloads a catalog of Bitbucket's methods and lets you turn on only the ones an agent needs: reading commits and pull requests, say, without enabling pushes or merges until there's an actual reason to. There's no .env file in this project either: Swytchcode keeps the account email and token pair in its own local store and reads them when a call runs, so the credential never ends up as a string the model assembles on its own.

Setting up Bitbucket with Swytchcode

swy search bitbucket
swy get bitbucket
swy list methods

Connect the account with swy auth connect and Swytchcode stores the email and token pair itself. Mint the token with just the scope the read methods need, and leave write scopes off the token entirely until a task genuinely calls for them.

Policies: build agents you can trust with write access

Scope the token to a single workspace and the narrowest permission that read access needs. Policy as code can deny the write methods (pushing, merging, creating a pull request) no matter what an agent is asked to do, until a person deliberately turns that on. That's a safer brake than trusting the prompt not to push something it shouldn't.

Bitbucket is in the middle of retiring app passwords in favor of API tokens, and the two authenticate with different usernames, which is exactly the kind of mismatch that breaks an agent's credentials silently instead of loudly. Swytchcode solves it by handling that credential pairing for you, scoping the token to only what's needed, and keeping write access behind policy instead of behind a prompt's good judgment.

FAQ

Can I still create a Bitbucket app password?

No. Atlassian stopped letting anyone create new app passwords on September 9, 2025, and disabled the ones that already existed on June 9, 2026. From that date, API tokens are the only Basic-auth option left for the REST API. Use an API token with scopes instead.

Which username do I send with an API token?

Your Atlassian account email, not your Bitbucket username. That's the app-password rule and it doesn't apply here. Atlassian's token page also lists x-bitbucket-api-token-auth as a valid static username for git operations specifically.

Will a token scoped to one workspace see a second workspace?

No. The workspace in the URL and the token's own workspace have to match. A 401 or an empty result on a different workspace is just the scope doing its job. Mint a second token if a second workspace is genuinely part of the agent's work.

What should I check first when an agent gets a 401?

In order: that the username is the Atlassian email (not the Bitbucket username), that the secret is an API token (not an app password or account password), that the token has at least one scope matching the endpoint, and that the token's workspace matches the one in the request URL. None of those checks require minting a wider token. Widening scope to make a 401 go away is how a bot ends up with access it was never supposed to have.

Can I build an AI agent on Bitbucket with Swytchcode?

Yes, connect the account once, enable the read methods a code agent needs (listing commits, reading pull requests, and so on), and leave push and merge off the catalog until a task genuinely requires them.

Can Swytchcode stop an agent from pushing or merging, even if it's asked to?

Yes. Policy can deny the write methods outright, independent of what the prompt asks for. An agent that's told to "push the fix" simply can't, if that method is denied at the policy level rather than left to the model's judgment.

If an agent retries a failed Bitbucket call, could that create a duplicate pull request?

It's a real risk worth planning for: Bitbucket's API doesn't document an idempotency-key mechanism the way a payments API would, so a retried write like opening a pull request can create a second one rather than being recognized as the same request. That's a reason to keep write methods gated by policy and to have the agent check for an existing open PR before creating another, rather than assuming a retry is always safe.

Use Swytchcode so this doesn't happen to you

Use Swytchcode so an agent never gets stuck on a 401 because it sent the Bitbucket username instead of the Atlassian email next to an API token.

Swytchcode is an execution kernel that sits between your AI agents and the APIs they call. It resolves the right method, injects auth, checks policy, retries safely with idempotency, and logs every call locally.

Start with the MCP quickstart, or browse the docs for the full reference. Free is 100 live commands a month forever; see pricing for Pro and Business.

More content