OpenCode

How to fix OpenCode 401 or an invalid API Key

First confirm that the request reached the service; 401 usually points to the credential, Provider, or config location.

Start with these checks

The request reached the service but authentication failed.

  • OpenCode returns 401, Unauthorized, or Invalid API Key.
  • The API Key is incomplete or expired
  • Copy a fresh API Key from AI Code With
401Unauthorized

01

Confirm the request reached the service

A 401 or Unauthorized response means the request arrived at the service, so check the API Key and Provider before changing the model.

02

Check the credential and Provider

In the generated JSON, confirm apiKey is inside the provider options and the Provider ID exactly matches the value generated on this page.

  1. 1Copy a fresh API Key from AI Code With
  2. 2Replace the placeholder with the complete key

03

Verify again

Save, reopen OpenCode, run /models, and send a short message. No 401 means the credential path is working.

  1. 1Replace the placeholder with the complete key
  2. 2Confirm the Provider ID matches the generated config

If it is still not fixed

Sources

Reconfigure

Return to the integration center and continue the setup steps.

Return to the related setup step