Lecca.io LogoLecca.io
Connections

Create OAuth2 Connection

We are working on a CLI to make this easier

Create connections folder

Locate the your app folder in the apps directory and add a new connections folder if it doesn't exist already.

your-new-app.oauth2.ts
your-new-app.app.ts

Create connection file

Create a new file within your app's connection folder. For an oauth2 connection, call it your-new-app.oauth2.ts, replacing your-new-app with your app id (which should match your app file).

Add your own values. We provided mock data to help initialize the process.

import { createOAuth2Connection } from "@lecca-io/toolkit";
 
/**
 * Note that the createOAuth2Connection function is completely typed
 * and has a lot of optional arguments like changing the scope
 * delimeter, adding more auth headers, auth params, pkce, .etc.
 */
export const yourNewAppOAuth2 = createOAuth2Connection({
  id: "your-new-app_connection_oauth2",
  name: "OAuth2",
  description: "Connect using OAuth2",
  authorizeUrl: "https://accounts.google.com/o/oauth2/v2/auth", //Replace with correct url
  tokenUrl: "https://oauth2.googleapis.com/token", //Replace with correct url
  getClientId: () => process.env.INTEGRATION_YOUR_NEW_APP_CLIENT_ID, //Replace `YOUR_NEW_APP` with your app name
  getClientSecret: () => process.env.INTEGRATION_YOUR_NEW_APP_CLIENT_SECRET, //Replace `YOUR_NEW_APP` with your app name
  scopes: [],
});
  1. export const yourNewAppOAuth2 replacing yourNewApp with your app name.

  2. Add an id: your-new-app_connection_oauth2

    • Make sure the id follows the format <app-id>_connection_oauth2
  3. Add the authorize url.

    This is a url that will be sent to the client to redirect the user to start the auth flow. For example, https://accounts.google.com/o/oauth2/v2/auth

  4. Add the tokenUrl.

    This is used by the server to retrieve access tokens, refresh tokens, and to refresh the tokens. For example, https://oauth2.googleapis.com/token

  5. Add the client id and client secret.

    You will need to create an account on the third party platform and follow their instructions to generate these values.

    Once you have the values, you will need to add them as environment variables.

    To keep the client secret and client id secrets consistent accross the platform, prefix the variable name with INTEGRATION_ every time.

  6. Add the required scopes to perform the api calls needed for actions and triggers.

If you ever need to update these scopes, all connections created with the old scopes will still work, but not have the new scopes. We currently don't have a way to refresh old connections with new scopes. So try to make sure you have all scopes you need and think will be needed in the near future.

For example, don't just get the read scope, get the write scope as well. Because even if you only plan on adding a read action, it is very likely someone will want to add the write action in the future.

This being said, don't add all the scopes. This is usually a good discussion you can start so that multiple people can determine what scopes are most likely to be needed for the app.

You can always create multiple oauth2 connections with different levels of access. Though we don't really have a good UX for that and could lead to confusion.

Add new connection to the app's connections property

your-new-app.app.ts
import { createApp } from "@lecca-io/toolkit";
 
export const yourNewApp = createApp({
  ...
  actions: [],
  triggers: [],
  connections: [yourNewAppOAuth2], 
});

On this page