Persistent Label Studio API Connection Issue from Python - 401 "Invalid token" Error

Hello everyone,

I’m encountering a consistent issue when trying to connect to the Label Studio API from a Python script using the label-studio-sdk. I’m repeatedly getting a 401 Client Error: Unauthorized with the detail “Invalid token,” despite having tried all known troubleshooting steps.

Problem Details:

  • Label Studio URL: http://127.0.0.1:8087 (server runs with --port 8087 --internal-host 127.0.0.1).
  • Python script output:
    • The initial connection test succeeds: Server responds at http://127.0.0.1:8087 with status code 200 (OK). Seems promising!
    • However, when attempting to access the API (e.g., client.get_projects()), I receive an HTTPError: 401 Client Error: Unauthorized for url: http://127.0.0.1:8087/api/projects?page_size=10000000 with the content { "detail": "Invalid token." }.

Steps I’ve Already Taken (Unsuccessfully):

  1. Label Studio Server Verification:
  • The server is running and accessible via browser at http://127.0.0.1:8087.
  • The Python script receives a 200 OK response from the base URL.
  1. Firewall/Connection Issues:
  • Configured Outbound Rules in Windows Defender Firewall to allow Python to connect to port 8087.
  • Test-NetConnection works correctly.
  • The 200 OK response confirms that the basic connection works and there are no network blocks.
  1. API Key (Multiple Careful Attempts):
  • I have copied and pasted the API Key dozens of times with extreme care, including using the “Copy” button in the UI and pasting into a plain text editor to avoid spaces or hidden characters.
  • I deleted all existing tokens in “Account & Settings” → “Access Tokens” and generated a completely new token for my user (who has an Owner role).
  • No environment variables (like HOST) are set to interfere with server binding.
  • I even tried creating a brand new user account in Label Studio and generating a fresh API Key for that user, but still encountered the same “Invalid token” error.
  • The SDK example code is functionally identical to what I’m using.
  1. Label Studio SDK Version:
  • pip install --upgrade label-studio-sdk has been performed.

Despite all these checks, the “Invalid token” error persists. It seems the issue is specifically with the API Key recognition, even though it appears to be copied correctly from the Label Studio UI.

Any help or further guidance would be greatly appreciated.

Hello, can you try this:

  1. Go to your organization page > Access Token Settings > disable “Personal Access Tokens”
  2. Go to user account page and copy your Legacy Token
  3. Insert it to your script.

Will it work?

Thanks so much for the solution! It’s working for me.