Skip to content

heroku-reference-apps/applink-getting-started-nodejs

Heroku AppLink Node.js App Template

Deploy

The Heroku AppLink Node.js app template is a Fastify web application that demonstrates how to build APIs for Salesforce integration using Heroku AppLink. This template includes authentication, authorization, and API specifications for seamless integration with Salesforce, Data Cloud, and Agentforce.

Table of Contents

Quick Start

Prerequisites

  • Node.js 20.x or later
  • npm
  • Git
  • Heroku CLI (for deployment)
  • Salesforce org (for AppLink integration)

Deploy to Heroku (One-Click)

Click the Deploy button above to deploy this app directly to Heroku with the AppLink add-on pre-configured.

Local Development

1. Clone and Install

git clone https://github.com/heroku-reference-apps/applink-getting-started-nodejs.git
cd applink-getting-started-nodejs
npm install

2. Start the Development Server

# Start with auto-reload and debug logging
npm run dev

# Or start production mode
npm start

Your app will be available at http://localhost:3000

3. API Endpoints

  • GET /accounts - Retrieve Salesforce accounts from the invoking org
  • POST /unitofwork - Create a unit of work for Salesforce
  • POST /handleDataCloudDataChangeEvent - Handle a Salesforce Data Cloud Change Event
  • GET /api-docs - Interactive Swagger UI for API documentation
  • GET /health - Health check endpoint

4. View API Documentation

Visit http://localhost:3000/api-docs to explore the interactive API documentation powered by Swagger UI.

Testing locallywith invoke.sh

The bin/invoke.sh script allows you to test your locally running app with proper Salesforce client context headers.

Usage

./bin/invoke.sh ORG_DOMAIN ACCESS_TOKEN ORG_ID USER_ID [METHOD] [API_PATH] [DATA]

Parameters

  • ORG_DOMAIN: Your Salesforce org domain (e.g., mycompany.my.salesforce.com)
  • ACCESS_TOKEN: Valid Salesforce access token
  • ORG_ID: Salesforce organization ID (15 or 18 characters)
  • USER_ID: Salesforce user ID (15 or 18 characters)
  • METHOD: HTTP method (default: GET)
  • API_PATH: API endpoint path (default: /accounts)
  • DATA: JSON data for POST/PUT requests

Examples

# Test the accounts endpoint
./bin/invoke.sh mycompany.my.salesforce.com TOKEN_123 00D123456789ABC 005123456789ABC

# Test with POST data
./bin/invoke.sh mycompany.my.salesforce.com TOKEN_123 00D123456789ABC 005123456789ABC POST /accounts '--data "{\"name\":\"Test Account\"}"'

# Test custom endpoint
./bin/invoke.sh mycompany.my.salesforce.com TOKEN_123 00D123456789ABC 005123456789ABC GET /health

Getting Salesforce Credentials

To get the required Salesforce credentials for testing:

  1. Access Token: Use Salesforce CLI to generate a session token
  2. Org ID: Found in Setup → Company Information
  3. User ID: Found in your user profile or Setup → Users

Manual Heroku Deployment

1. Prerequisites

  • Heroku CLI installed
  • Git repository initialized
  • Heroku account with billing enabled (for add-ons)

2. Create Heroku App

# Create a new Heroku app
heroku create your-app-name

# Or let Heroku generate a name
heroku create

3. Add Required Buildpacks

The app requires two buildpacks in the correct order:

# Add the AppLink Service Mesh buildpack first
heroku buildpacks:add heroku/heroku-applink-service-mesh

# Add the Node.js buildpack second
heroku buildpacks:add heroku/nodejs

4. Provision the AppLink Add-on

# Provision the Heroku AppLink add-on
heroku addons:create heroku-applink

# Set the required HEROKU_APP_ID config var
heroku config:set HEROKU_APP_ID="$(heroku apps:info --json | jq -r '.app.id')"

5. Deploy the Application

# Deploy to Heroku
git push heroku main

# Check deployment status
heroku ps:scale web=1
heroku open

6. Verify Deployment

# Check app logs
heroku logs --tail

Heroku AppLink Setup

1. Install AppLink CLI Plugin

# Install the AppLink CLI plugin
heroku plugins:install @heroku-cli/plugin-applink

2. Connect to Salesforce Org

# Connect to a production org
heroku salesforce:connect production-org --addon your-addon-name -a your-app-name

# Connect to a sandbox org
heroku salesforce:connect sandbox-org --addon your-addon-name -a your-app-name --login-url https://test.salesforce.com

3. Authorize a User

# Authorize a Salesforce user for API access
heroku salesforce:authorizations:add auth-user --addon your-addon-name -a your-app-name

4. Publish Your App

# Publish the app to Salesforce as an External Service
heroku salesforce:publish api-spec.yaml \
  --client-name MyAPI \
  --connection-name production-org \
  --authorization-connected-app-name MyAppConnectedApp \
  --authorization-permission-set-name MyAppPermissions \
  --addon your-addon-name

5. Required Salesforce Permissions

Users need the "Manage Heroku AppLink" permission in Salesforce:

  1. Go to Setup → Permission Sets
  2. Create a new permission set or edit existing
  3. Add "Manage Heroku AppLink" system permission
  4. Assign the permission set to users

6. AppLink Buildpack Details

The heroku/heroku-applink-service-mesh buildpack provides:

  • Service Mesh Proxy: Handles authentication and authorization
  • Request Validation: Validates incoming Salesforce requests
  • Header Management: Manages Salesforce client context headers
  • Security: Ensures secure communication between Salesforce and your app

The service mesh runs on a separate port and proxies requests to your app, adding the necessary Salesforce context.

7. Connecting to Different Org Types

Production/Developer Orgs

heroku salesforce:connect prod-org --login-url https://login.salesforce.com

Sandbox Orgs

heroku salesforce:connect sandbox-org --login-url https://test.salesforce.com

Data Cloud Integration

heroku datacloud:connect datacloud-org --addon your-addon-name -a your-app-name

JWT Authentication (CI/CD)

heroku salesforce:connect:jwt cicd-org \
  --client-id YOUR_CLIENT_ID \
  --jwt-key-file path/to/server.key \
  --username [email protected] \
  --login-url https://login.salesforce.com

Available Scripts

npm start          # Start production server
npm run dev        # Start development server with auto-reload
npm test           # Run test suite
npm run lint       # Run ESLint
npm run lint:fix   # Fix ESLint errors
npm run format     # Format code with Prettier

Additional Resources

Documentation

Integration Guides

Support


Note: This template is designed for educational purposes and as a starting point for building Salesforce-integrated applications. For production use, ensure proper error handling, security measures, and testing practices are implemented.

About

Heroku AppLink Sample App with Node.js and Fastify

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors 3

  •  
  •  
  •