VRoid Hub API
This flow is more complex than static hosting because VRoid Hub requires OAuth.
You cannot call /api/vrm/:avatar_id first. You must complete OAuth once in a browser and get a refresh token.
Overview
- Get a client ID and client secret from the VRoid Hub API.
- Write the obtained client ID and client secret to
VROID_APP_IDandVROID_CLIENT_SECRET. - Open
/api/authin your browser. - Sign in to VRoid Hub and approve access.
- VRoid Hub redirects to
/api/auth/callback. - The callback exchanges the authorization code for tokens.
- Save the returned refresh token to environment variables or KV.
- Now
/api/vrm/:avatar_idcan fetch JSON that contains a temporary download URL.
Code Example
The following examples are based on the code used by v.logue.dev. USER_AGENT is the constant defined in the first snippet.
/api/auth
/api/auth/callback
/api/vrm/:avatar_id
Required Environment Variables
VROID_APP_IDVROID_CLIENT_SECRET
Optional:
VROID_REFRESH_TOKENTOKEN_STORE(Cloudflare KV binding for token rotation)
Notes:
- In VRoid terms, the application key corresponds to
VROID_APP_ID(client ID). - In VRoid terms, the secret key corresponds to
VROID_CLIENT_SECRET(client secret). - Write the obtained client ID and client secret to
VROID_APP_IDandVROID_CLIENT_SECRET. VROID_CLIENT_SECRETis required for token exchange in callback and refresh flow.- If
TOKEN_STOREis configured, the latest refresh token is stored there and used first.
VRoid Hub App Settings
Create an OAuth app in VRoid Hub developer settings and configure callback URL:
- Local:
http://127.0.0.1:8788/api/auth/callback(or your local Worker URL) - Production:
https://your-domain.example/api/auth/callback
The callback URL must exactly match the URL used by the Worker.
Browser Step (Mandatory)
Open this URL in your browser:
/api/auth
What it does:
- Generates the PKCE
code_verifier/code_challenge. - Generates
statefor CSRF protection. - Stores both values in HttpOnly cookies.
- Redirects to VRoid Hub OAuth authorize endpoint.
After approval, VRoid Hub redirects to:
/api/auth/callback?code=...&state=...
The callback endpoint validates state and code_verifier, then exchanges the code for tokens.
How to Get the Refresh Token
When callback succeeds, the response includes a line like:
VROID_REFRESH_TOKEN=xxxxxxxx
Use this value as your initial refresh token.
If TOKEN_STORE is configured, the callback also stores the refresh token in KV.
Call the Avatar Endpoint
After OAuth setup, call:
/api/vrm/{avatar_id}
Example:
/api/vrm/your_avatar_id
The endpoint flow is:
- Load refresh token from KV (fallback to
VROID_REFRESH_TOKEN). - Exchange refresh token for access token (and rotated refresh token).
- Query account character models and find the model by
avatar_id. - Issue a download license.
- Get JSON from VRoid Hub that includes a temporary URL.
- Return JSON:
{ "url": "..." }to the client.
The client then calls fetch(url) and reads the VRM data as an ArrayBuffer:
Typical Pitfalls
- Callback URL mismatch in VRoid Hub app settings.
- Calling
/api/vrm/:avatar_idbefore completing/api/authbrowser flow. - Trying to open the temporary VRM URL directly in a browser instead of fetching it from the app.
- Missing
VROID_CLIENT_SECRET. - No
TOKEN_STOREbinding when relying on automatic refresh token rotation.
Quick Test Sequence
- Set
VROID_APP_IDandVROID_CLIENT_SECRET. - Open
/api/authin browser and complete consent. - Confirm callback success and copy
VROID_REFRESH_TOKEN. - Save token to env (or confirm KV write).
- Call
/api/vrm/{avatar_id}.