--- name: bring-your-own-pollen description: Add Pollinations Pay so each user can pay from their own balance. Use when building an app, CLI, extension or agent tool on Pollinations and you need the user-authorized sk_ key, when you must choose between the OAuth code flow, the fragment flow and the device flow, or when an app key's usage must be attributed and monetized. --- # Pollinations Pay for apps Pollinations Pay lets users pay for your app's requests from their Pollinations balance. Each user connects their account and approves access, then the app receives a scoped user key (`sk_...`) for API requests. Users control the budget and expiry and can revoke access at any time. [Pollinations Pay guide](https://gen.pollinations.ai/docs/llm.txt?section=byop) · [OAuth discovery](https://enter.pollinations.ai/.well-known/oauth-authorization-server) ## Pick the flow first | Your app | Flow | Who exchanges the code | | --- | --- | --- | | Web app with a backend | OAuth authorization-code + PKCE | Backend (keeps secret) | | Static / browser-only web app | OAuth authorization-code + PKCE | Browser via `fetch` (PKCE replaces the secret) | | Legacy web app | Fragment redirect | No exchange; key arrives in `#api_key=` | | CLI, bot, extension, anything headless | Device flow | The app polls the token endpoint | If you are unsure, default to the **OAuth code flow with PKCE** for new web work; it keeps `sk_...` out of the browser callback URL. ## Step 1 — App Key (`client_id`, optional) Every flow, including the `` drop-in, works without an App Key: the consent screen then shows the redirect hostname and usage isn't attributed or monetized. For a quick static app, just add the button: ```html ``` Never ask visitors to paste an `sk_` key, or to create a `pk_` key themselves. To show your app's name and earn, create one at `https://enter.pollinations.ai/keys?tab=app` → **App** → **Create**, or via the API: ```bash curl -X POST https://gen.pollinations.ai/account/keys \ -H 'Authorization: Bearer sk_yoursecretkey' \ -H 'Content-Type: application/json' \ -d '{"name":"my-app","type":"publishable","redirectUris":["https://myapp.com/callback"],"earningsEnabled":true}' ``` - `redirectUris` must match the callback **exactly**, query string included. Loopback `http://localhost` matches any port. - `earningsEnabled: true` opts into developer earnings: users pay 25% over base rates and the markup credits **your** balance, in the same bucket the user paid from. ## Step 2 — OAuth code flow with PKCE Build the auth link (generate a fresh verifier per attempt): ```text https://enter.pollinations.ai/authorize ?response_type=code &client_id=pk_yourkey &redirect_uri=https://myapp.com/callback &scope=profile%20usage &state=random-csrf-token &code_challenge=BASE64URL_SHA256_VERIFIER &code_challenge_method=S256 ``` Optional restrictions: `models=image,text` (or individual IDs), `budget=10` (spending cap in dollars, default 5, at most 10 from a link), `expiry=7` (days, default 7). The user can change these on the consent screen. A requested `machines` scope starts unticked there; `keys` is only offered in the device flow and on redirects to `http://localhost` or `http://127.0.0.1`. Handle the callback — validate `state`, then exchange the single-use code (10-minute TTL). Send the same `client_id` as on the auth link, or none: ```bash curl -X POST https://enter.pollinations.ai/api/oauth/token \ -H 'Content-Type: application/x-www-form-urlencoded' \ -d 'grant_type=authorization_code' \ -d 'code=oauth_code' \ -d 'client_id=pk_yourkey' \ -d 'redirect_uri=https://myapp.com/callback' \ -d 'code_verifier=YOUR_PKCE_VERIFIER' # -> { "access_token": "sk_...", "token_type": "bearer", "expires_in": 604800, "scope": "profile usage" } ``` There are **no refresh tokens** — re-run the flow when the key expires. Store the token in memory or `sessionStorage`; never in `localStorage`, a URL, analytics or logs. ## Step 3 — Call the API with the user key ```javascript await fetch('https://gen.pollinations.ai/v1/chat/completions', { method: 'POST', headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ model: 'openai/gpt-5.4-nano', messages: [{ role: 'user', content: 'yo' }] }), }); ``` Generation needs **no scope** — spending is bounded by the `budget` and `expiry` the user approved. Scopes gate account data only: `profile` (name + email), `usage` (balance + usage), `keys` (create/list/revoke keys; device flow and localhost redirects only). ## Device flow — the headless path Use this for CLIs, bots and extensions that cannot show the consent page. ```bash # 1. request a device code (pass client_id for attribution) curl -X POST https://enter.pollinations.ai/api/device/code \ -H 'Content-Type: application/json' \ -d '{"client_id": "pk_yourkey"}' # -> { "device_code": "...", "user_code": "ABCD-1234", # "verification_uri": "https://enter.pollinations.ai/device", # "verification_uri_complete": "https://enter.pollinations.ai/device?user_code=ABCD-1234", # "expires_in": 1800, "interval": 5 } # 2. show verification_uri_complete to the user, then poll every `interval` seconds curl -X POST https://enter.pollinations.ai/api/device/token \ -H 'Content-Type: application/json' \ -d '{"device_code": "..."}' # pending -> { "error": "authorization_pending" } # done -> { "access_token": "sk_...", "token_type": "bearer" } ``` The same exchange is available at the standard endpoint with `grant_type=urn:ietf:params:oauth:grant-type:device_code`. ## Fragment flow (legacy web) ```text https://enter.pollinations.ai/authorize?redirect_uri=https://myapp.com/callback&client_id=pk_yourkey&scope=usage ``` The key returns in the URL fragment (`#api_key=sk_...&state=...`), so it never hits server logs. On denial the fragment is `#error=access_denied`. ```javascript const apiKey = new URLSearchParams(location.hash.slice(1)).get('api_key'); ``` ## Identify the key holder ```bash curl https://enter.pollinations.ai/api/device/userinfo \ -H 'Authorization: Bearer sk_...' # -> { "sub": "user-id", "preferred_username": "voodoohop", "picture": "..." } # with `profile` scope: also "name", "email" ``` ## Checklist - [ ] If you use an App Key: exact `redirect_uri` registered, and `earningsEnabled` if you monetize. - [ ] Fresh PKCE verifier + S256 challenge per attempt. - [ ] `state` validated on the callback. - [ ] Token kept in memory/`sessionStorage`; expiry handled by re-running the flow. - [ ] Device flow used for headless apps, polling at the returned `interval`.