I want to use roboflow api on my render server but it kept making errors.
It’s because of unauthorized error.
There must be a problem with my workspace.
Please check it.
I generated new key and make sure it’s clear and I copied well and it’s my workspace key.
But, it didn’t fixed again and again.
I need help.
I don’t know how to give permission to anyone but I can just tell my workspace/ Project ID
recycling-segmentation-amso6
recycling-segmentation-amso6/34
model version34 is the best performance so I want to use it.
Following is my error code
ERROR HTTPCallErrorError(description='401 Client Error: Unauthorized for url: https://serverless.roboflow.com/recycling-segmentation-amso6/34?api_key=I filled it&disable_active_learning=False’, api_message=‘Unauthorized access to roboflow API - check API key and make sure the key is valid for workspace you use. status_code=401)
Please help me.
And This is my fastapi code.
Please check this code.
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 Roboflow client
CLIENT = InferenceHTTPClient(
api_url="https://serverless.roboflow.com",
api_key="I filled it"
)
@app.get("/")
async def root():
return {"message": "Roboflow API Server", "status": "running"}
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
try:
# Save uploaded file 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:
# Analyze image with Roboflow API
result = CLIENT.infer(
temp_file_path,
model_id="recycling-segmentation-amso6/34"
)
return result
except Exception as api_error:
error_msg = str(api_error)
if "timeout" in error_msg.lower():
return {"error": "API server response 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)
Thank yor for reading my long article.
I must finish this project.
Please help me.