Skip to content

Quickstart

This guide walks you through connecting a user’s Google account via OAuth and retrieving a valid access token.

Terminal window
npm install @authpipe/node

Sign up at authpipe.dev and create a workspace. You’ll get a secret API key (starts with sk_) and a publishable key (starts with pk_).

  • Secret key — Full API access. Use server-side only.
  • Publishable key — Can only create auth sessions. Safe for client-side use.

Register your Google OAuth app credentials with Authpipe.

import { Authpipe } from "@authpipe/node";
const authpipe = new Authpipe({
apiKey: process.env.AUTHPIPE_API_KEY, // sk_...
});
const config = await authpipe.createProviderConfig({
provider_id: "google",
client_id: "your-google-client-id",
client_secret: "your-google-client-secret",
oauth_redirect_url: "https://api.authpipe.dev/oauth/callback",
attachment: "user",
enabled: true,
scopes: ["https://www.googleapis.com/auth/drive.readonly"],
});

The oauth_redirect_url must match what you registered in the Google Cloud Console. This is Authpipe’s callback endpoint, not your app’s.

When a user clicks “Connect Google,” create an auth session and redirect them.

const session = await authpipe.createAuthSession({
provider: "google",
tenantId: "org_acme",
userId: "user_123",
redirectUrl: "https://yourapp.com/connections/success",
});
// Redirect the user to session.authorization_url
res.redirect(session.authorization_url);

Authpipe handles the OAuth callback, exchanges the code for tokens, encrypts and stores them, then redirects the user to your redirect_url.

Retrieve a valid access token whenever you need to call the Google API.

const { credential, credential_type, source } = await authpipe.getCredential({
provider: "google",
tenantId: "org_acme",
userId: "user_123",
});
// Use the credential to call Google's API
const resp = await fetch("https://www.googleapis.com/drive/v3/files", {
headers: { Authorization: `Bearer ${credential}` },
});

If the token has expired, Authpipe refreshes it automatically before returning it.