|
| 1 | +# Draft Queue: a Buffer app client example |
| 2 | + |
| 3 | +A small, runnable example of a third-party app built on the [Buffer API](https://developers.buffer.com). |
| 4 | + |
| 5 | + |
| 6 | +## What it does |
| 7 | +It's a small starter app called Draft Queue which covers key components of an app client: |
| 8 | + |
| 9 | +1. A user connects their own Buffer account over OAuth 2.0 (Authorization Code + PKCE). |
| 10 | +2. The app lists the channels in their Buffer organization. |
| 11 | +3. The user picks a channel, and the app creates a **draft** post in that channel's queue. |
| 12 | + |
| 13 | +Use it as a starting point for your own integration. Clone it, configure it with your own app client, build new features on top, or share it with your AI coding agent as a reference. |
| 14 | + |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- Node.js 22 |
| 19 | +- A [Buffer](https://buffer.com) account |
| 20 | +- A Buffer app client (client ID and secret). See the [authentication guide](https://developers.buffer.com/guides/authentication.html) for how to register one. |
| 21 | +- (Optional) Vercel or another hosting provider, if you want to deploy the app |
| 22 | + |
| 23 | +## Setup |
| 24 | + |
| 25 | +1. From the root of the repository, move into this sample: |
| 26 | + |
| 27 | + ```bash |
| 28 | + cd app-client-starter-app |
| 29 | + ``` |
| 30 | + |
| 31 | +2. Install dependencies: |
| 32 | + |
| 33 | + ```bash |
| 34 | + npm install |
| 35 | + ``` |
| 36 | + |
| 37 | +3. Create your environment file: |
| 38 | + |
| 39 | + ```bash |
| 40 | + cp .env.example .env |
| 41 | + ``` |
| 42 | + |
| 43 | + Then fill in the values: |
| 44 | + |
| 45 | + | Variable | Purpose | |
| 46 | + | --- | --- | |
| 47 | + | `BUFFER_CLIENT_ID` | Your app client ID from Buffer. | |
| 48 | + | `BUFFER_CLIENT_SECRET` | Required for a confidential client. Never expose it to the browser or commit it. | |
| 49 | + | `SESSION_SECRET` | At least 32 characters. Generate one with `openssl rand -hex 32`. Optional locally, required in production. | |
| 50 | + | `REDIRECT_URI` | Optional locally and on your Vercel production deployment. If set, it must exactly match the redirect URI registered for your app client. Vercel preview deployments have no default callback, so login is disabled on them unless you register the preview's `/callback` URL and set `REDIRECT_URI` for that environment. | |
| 51 | + |
| 52 | +4. Generate a self-signed certificate for local HTTPS: |
| 53 | + |
| 54 | + ```bash |
| 55 | + npm run certs |
| 56 | + ``` |
| 57 | + |
| 58 | + Buffer doesn't accept `http://localhost` redirect URIs, so the dev server runs over HTTPS. If you skip this step, `npm start` generates the certificate on first run. |
| 59 | + |
| 60 | +## Run it locally |
| 61 | + |
| 62 | +```bash |
| 63 | +npm start |
| 64 | +``` |
| 65 | + |
| 66 | +Open `https://localhost:3000` and accept the self-signed development certificate in your browser. |
| 67 | + |
| 68 | +To run the tests and syntax checks: |
| 69 | + |
| 70 | +```bash |
| 71 | +npm test |
| 72 | +npm run lint |
| 73 | +``` |
| 74 | + |
| 75 | +## Project structure |
| 76 | + |
| 77 | +| Path | What it holds | |
| 78 | +| --- | --- | |
| 79 | +| `lib/` | The app itself: OAuth and GraphQL (`buffer.js`), the session cookie (`session.js`), Express routes (`app.js`), and configuration (`config.js`). | |
| 80 | +| `public/` | The single-page UI. | |
| 81 | +| `server.js` | Local HTTPS dev server. | |
| 82 | +| `api/index.js`, `vercel.json` | Serverless entry point and routing, using Vercel as an example host. | |
| 83 | +| `test/` | Regression tests for the protocol and security-critical helpers. | |
| 84 | +| `docs/` | Architecture notes and the production OAuth guide. | |
| 85 | +| `scripts/` | Generates the self-signed certificate for local HTTPS. | |
| 86 | + |
| 87 | +## What this example covers |
| 88 | + |
| 89 | +- **OAuth 2.0 with PKCE:** the full Authorization Code flow with PKCE. It sends the user to Buffer to authorize the app, handles the redirect back, and exchanges the authorization code for tokens. See `lib/buffer.js`. |
| 90 | +- **GraphQL requests:** authenticated calls to the Buffer GraphQL API. The app reads the user's organization and channels, then runs a mutation that creates a draft post in a channel's queue. |
| 91 | +- **Refresh tokens:** the app requests the `offline_access` scope, so Buffer returns a refresh token alongside the access token. When the access token expires, the app renews it without asking the user to sign in again. Both tokens are sealed inside an HttpOnly cookie, so browser JavaScript can never read them. |
| 92 | +- **Staying within rate limits:** the app reads the rate limit headers on each response and uses them to stay within the limits applied to the client. |
| 93 | +- **Deployment:** a working setup for Vercel (`api/index.js` and `vercel.json`) shows how to run the same app as a serverless deployment. You can adapt it to any Node.js host. |
| 94 | + |
| 95 | +## Further reading |
| 96 | + |
| 97 | +- [Buffer developer docs](https://developers.buffer.com) |
| 98 | +- [Architecture notes](docs/architecture.md) |
| 99 | +- [Production OAuth guide](docs/production-oauth.md) |
| 100 | + |
| 101 | +## Support and contributing |
| 102 | + |
| 103 | +This sample is provided as-is for educational purposes and isn't covered by Buffer's product support or service-level agreements. If you find a bug or something out of date, please [open an issue](../../../issues). See the [repository README](../README.md#contributing) for how we handle contributions. |
| 104 | + |
| 105 | +## License |
| 106 | + |
| 107 | +This sample is licensed under the MIT License. See the [LICENSE](../LICENSE) file at the root of the repository. |
0 commit comments