How to access Inference API using connected developer app

I have set up a connected developer app, and my website successfully authenticates via HF login auth flow. I can get some access tokens through the openID api auth flow, but none of these are HF_TOKEN (it appears) for the inference api.

I have enabled inference-api in the permissions of the connected application in HF (Hugging Face – The AI community building the future.).

Since the inference-api setting exists in the connected application permissions, I assume HF intends to allow users to access the HF inference api on other services and be charged for them, but maybe this is not the case?

How do I get the authenticated HF users’ HF_TOKEN to use from my web app, with the inference API?

Thanks for any help!

1 Like

How about this?
This is one part of my code.

from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
import requests
import os

app = FastAPI()

# Use your own HF token stored in environment variables
HF_TOKEN = os.getenv("HF_TOKEN")
HF_API_URL = "https://api-inference.huggingface.co/models"

class InferenceRequest(BaseModel):
    model: str
    inputs: dict

def get_hf_headers():
    return {"Authorization": f"Bearer {HF_TOKEN}"}

@app.post("/run-inference/")
def run_inference(request: InferenceRequest):
    model_url = f"{HF_API_URL}/{request.model}"
    response = requests.post(model_url, json=request.inputs, headers=get_hf_headers())

    if response.status_code != 200:
        raise HTTPException(status_code=response.status_code, detail=response.json())

    return response.json()

1 Like