> For the complete documentation index, see [llms.txt](https://docs.floral.fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.floral.fun/integration-guides/connect-with-floral.md).

# Connect with Floral button

This complete example links a Floral account from a Node.js server, using a confidential client, Express and PKCE.

## Set up

```bash
npm install express express-session
export FLORAL_CLIENT_ID=flr_client_...
export FLORAL_CLIENT_SECRET=flr_secret_...
export SESSION_SECRET=a-long-random-string
```

Register `http://localhost:3000/auth/floral/callback` as a redirect URL.

## Server

```javascript
import { createHash, randomBytes } from "node:crypto";
import express from "express";
import session from "express-session";

const API = "https://api.floral.fun";
const REDIRECT_URI = "http://localhost:3000/auth/floral/callback";
const { FLORAL_CLIENT_ID, FLORAL_CLIENT_SECRET, SESSION_SECRET } = process.env;

const app = express();
app.use(session({ secret: SESSION_SECRET, resave: false, saveUninitialized: false }));

app.get("/", (req, res) => {
  res.send(req.session.floral
    ? `Connected as @${req.session.floral.username}`
    : '<a href="/auth/floral">Connect with Floral</a>');
});

// 1. Send the user to Floral.
app.get("/auth/floral", (req, res) => {
  const verifier = randomBytes(32).toString("base64url");
  const state = randomBytes(16).toString("base64url");
  req.session.oauth = { verifier, state };

  const url = new URL(`${API}/oauth/authorize`);
  url.search = new URLSearchParams({
    response_type: "code",
    client_id: FLORAL_CLIENT_ID,
    redirect_uri: REDIRECT_URI,
    scope: "profile:read quests:verify",
    state,
    code_challenge: createHash("sha256").update(verifier).digest("base64url"),
    code_challenge_method: "S256",
  }).toString();
  res.redirect(url.toString());
});

// 2. Floral sends them back with a code.
app.get("/auth/floral/callback", async (req, res) => {
  const pending = req.session.oauth;
  delete req.session.oauth;
  if (!pending || req.query.state !== pending.state) return res.status(400).send("Invalid state");
  if (req.query.error) return res.redirect("/");

  const tokenResponse = await fetch(`${API}/oauth/token`, {
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization: `Basic ${Buffer.from(`${FLORAL_CLIENT_ID}:${FLORAL_CLIENT_SECRET}`).toString("base64")}`,
    },
    body: new URLSearchParams({
      grant_type: "authorization_code",
      code: String(req.query.code),
      redirect_uri: REDIRECT_URI,
      code_verifier: pending.verifier,
    }),
  });
  if (!tokenResponse.ok) return res.status(502).send("Couldn't connect to Floral");
  const tokens = await tokenResponse.json();

  // 3. Read who connected. Store profile.id, because usernames can change.
  const profileResponse = await fetch(`${API}/v1/users/me`, {
    headers: { Authorization: `Bearer ${tokens.access_token}` },
  });
  const profile = await profileResponse.json();

  // Keep tokens server-side. Encrypt them at rest in a real database.
  req.session.floral = { id: profile.id, username: profile.username, ...tokens };
  res.redirect("/");
});

app.listen(3000, () => console.log("Open http://localhost:3000"));
```

Save it as `server.mjs` and run `node server.mjs`.

## Refresh tokens

Access tokens last an hour. When a call returns `401`, [refresh it](/authentication/floral-connect.md#refresh-a-token) and store the new refresh token. If refreshing returns `invalid_grant`, the user has disconnected or your tokens were revoked, so show the Connect button again.

## Verify a quest

```javascript
const response = await fetch(`${API}/v1/quests/verify`, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${accessToken}`,
  },
  body: JSON.stringify({
    action: "like_post",
    target_id: "post_456",
    campaign_id: "spring-launch",
    campaign_start: "2026-11-03T00:00:00Z",
  }),
});
const result = await response.json();
if (result.verified && !result.already_verified) awardPoints(result.user_id);
```

## Using a public client

A browser or mobile app follows the same steps with these differences:

* Store the verifier and `state` on the device, for example in `sessionStorage`.
* Leave out the `Authorization` header, and send `client_id` in the token request body. `/oauth/token`, `/oauth/revoke` and `/v1` allow direct browser calls.
* Keep tokens out of URLs and logs.
* On mobile, use an https universal link or app link as the redirect URL, and open the authorization page in the system browser.

```javascript
const tokenResponse = await fetch("https://api.floral.fun/oauth/token", {
  method: "POST",
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  body: new URLSearchParams({
    grant_type: "authorization_code",
    client_id: CLIENT_ID,
    code,
    redirect_uri: REDIRECT_URI,
    code_verifier: verifier,
  }),
});
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.floral.fun/integration-guides/connect-with-floral.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
