HTTPCallErrorError(description=‘401 Client Error: Unauthorized)

Project Type: Semantic Segmentation
Operating System & Browser: Windows/Chrome
Project ID: Recycling Segmentation
Model ID: recycling-segmentation-amso6/28
This error message occurs every single time when I run android studio code which is written by flutter.
HTTPCallErrorError(description='401 Client Error: Unauthorized.
I revoke my api key and I also refresh my render server which is model server website connected by github
I followed the instruction and copied the Hosted Image Inference code but it didn’t work
I think you forgot to reply my answer.
So I reuploaded this topic.
Following code is my code snippet.

from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from inference_sdk import InferenceHTTPClient
import uvicorn
import os
import tempfile

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["GET", "POST"],
    allow_headers=["*"],
)

# initialize the client
CLIENT = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key="My own api key"
)

@app.get("/")
async def root():
    return {"message": "Roboflow API Server", "status": "running"}

@app.post("/predict")
async def predict(file: UploadFile = File(...)):
    try:
        # Save uploaded image as temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=f".{file.filename.split('.')[-1]}") as temp_file:
            content = await file.read()
            temp_file.write(content)
            temp_file_path = temp_file.name
        
        try:
            # Inference on local image
            result = CLIENT.infer(
                temp_file_path, 
                model_id="recycling-segmentation-amso6/28"  
            )
            return result
            
        except Exception as api_error:
            error_msg = str(api_error)
            if "timeout" in error_msg.lower():
                return {"error": "API server timeout", "status": "failed"}
            elif "connection" in error_msg.lower():
                return {"error": "Network connection failed", "status": "failed"}
            elif "401" in error_msg or "unauthorized" in error_msg.lower():
                return {"error": "API key authentication failed", "status": "failed"}
            elif "404" in error_msg:
                return {"error": "Model not found", "status": "failed"}
            else:
                return {"error": str(api_error), "status": "failed"}
                
        finally:
            # Delete temporary file
            if os.path.exists(temp_file_path):
                os.unlink(temp_file_path)
            
    except Exception as e:
        return {"error": str(e), "status": "failed"}

@app.post("/predict-raw")
async def predict_raw(file: UploadFile = File(...)):
    return await predict(file)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 8000))
    uvicorn.run("main:app", host="0.0.0.0", port=port, reload=False)

Hi @Minyst!
Unfortunately I’m unable to debug code outside of the Roboflow platform. Given your 401 client error, it is likely an issue with your API key.

Are you possibly using an API key from a different workspace?

I generate new workspace and transfer my data from old one to new one but it didn’t work.

It occured same error