Connect users

A user approves a spending limit for your app once, on our connect page. After that you can charge them.

The flow

  1. You send the user to the connect page.
  2. They sign in with email, choose a limit and approve it.
  3. They come back to your returnTo URL with a one-time code.
  4. Your server swaps the code for their NEAR account and email.
app.get("/connect", async (req, res) => {
  res.redirect(await credits.connectUrl({ credits: 200, returnTo: "https://your.app/callback" }));
});
 
app.get("/callback", async (req, res) => {
  const { user, email } = await credits.exchangeCode(String(req.query.code));
  req.session.creditsUser = user; // charge this account from now on
  req.session.creditsEmail = email; // shown to the user, never used to charge
  res.redirect("/");
});

The code works once, for your app only, for 10 minutes. Always swap it on your server, never trust an account sent from the browser.

Check a user

const u = await credits.user(account);
// { balance, connected, limit, spendable }

spendable is what you can charge right now.

Out of credits

Send them to the buy page. They come back to returnTo when they're done.

res.redirect(credits.buyUrl({ credits: 500, returnTo: "https://your.app/" }));

In test mode

There's no connect page in test mode. Make a code from any account:

const { user } = await credits.exchangeCode(`cc_test_${account}`);

Using React? The React kit does all of this in a popup.