|
1 | | -# FastAPI Heroku AppLink Integration |
| 1 | +# Heroku AppLink Python App Template |
2 | 2 |
|
3 | | -This is a basic FastAPI application that demonstrates integration with Heroku AppLink. |
| 3 | +[](https://www.heroku.com/deploy?template=https://github.com/heroku-reference-apps/applink-getting-started-python) |
4 | 4 |
|
5 | | -## Local Setup |
| 5 | +The Heroku AppLink Python app template is a [FastAPI](https://fastapi.tiangolo.com/) 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. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Quick Start](#quick-start) |
| 10 | +- [Local Development](#local-development) |
| 11 | +- [Testing with invoke.py](#testing-with-invokepy) |
| 12 | +- [Running Automated Tests](#running-automated-tests) |
| 13 | +- [Manual Heroku Deployment](#manual-heroku-deployment) |
| 14 | +- [Heroku AppLink Setup](#heroku-applink-setup) |
| 15 | +- [Project Structure](#project-structure) |
| 16 | +- [API Documentation](#api-documentation) |
| 17 | +- [Additional Resources](#additional-resources) |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | + |
| 23 | +- Python 3.8+ |
| 24 | +- `pip` for package management |
| 25 | +- Git |
| 26 | +- Heroku CLI (for deployment) |
| 27 | +- Salesforce org (for AppLink integration) |
| 28 | + |
| 29 | +### Deploy to Heroku (One-Click) |
| 30 | + |
| 31 | +Click the Deploy button above to deploy this app directly to Heroku with the AppLink add-on pre-configured. |
| 32 | + |
| 33 | +## Local Development |
| 34 | + |
| 35 | +### 1. Clone and Install |
6 | 36 |
|
7 | | -1. Install the required dependencies: |
8 | 37 | ```bash |
| 38 | +git clone https://github.com/heroku-reference-apps/applink-getting-started-python.git |
| 39 | +cd applink-getting-started-python |
| 40 | +python -m venv venv |
| 41 | +source venv/bin/activate |
9 | 42 | pip install -r requirements.txt |
10 | 43 | ``` |
11 | 44 |
|
12 | | -2. Run the application: |
| 45 | +### 2. Start the Development Server |
| 46 | + |
13 | 47 | ```bash |
14 | | -uvicorn main:app --reload |
| 48 | +uvicorn app.main:app --reload |
15 | 49 | ``` |
16 | 50 |
|
17 | | -The application will be available at `http://localhost:8000` |
| 51 | +Your app will be available at `http://localhost:8000`. |
| 52 | + |
| 53 | +### 3. API Endpoints |
| 54 | + |
| 55 | +- **GET /accounts** - Retrieve Salesforce accounts from the invoking org. |
| 56 | +- **POST /unitofwork** - Create a unit of work for Salesforce. |
| 57 | +- **POST /handleDataCloudDataChangeEvent** - Handle a Salesforce Data Cloud Change Event. |
| 58 | +- **GET /docs** - Interactive Swagger UI for API documentation. |
| 59 | +- **GET /health** - Health check endpoint. |
| 60 | + |
| 61 | +### 4. View API Documentation |
| 62 | + |
| 63 | +Visit `http://localhost:8000/docs` to explore the interactive API documentation powered by Swagger UI. |
18 | 64 |
|
19 | | -## Heroku Deployment |
| 65 | +## Testing with invoke.py |
| 66 | + |
| 67 | +The `bin/invoke.py` script allows you to test your locally running app with proper Salesforce client context headers. |
| 68 | + |
| 69 | +### Usage |
20 | 70 |
|
21 | | -1. Make sure you have the Heroku CLI installed and are logged in: |
22 | 71 | ```bash |
23 | | -heroku login |
| 72 | +./bin/invoke.py ORG_DOMAIN ACCESS_TOKEN ORG_ID USER_ID [METHOD] [API_PATH] [--data DATA] |
24 | 73 | ``` |
25 | 74 |
|
26 | | -2. Create a new Heroku app: |
| 75 | +### Parameters |
| 76 | + |
| 77 | +- **ORG_DOMAIN**: Your Salesforce org domain (e.g., `mycompany.my.salesforce.com`) |
| 78 | +- **ACCESS_TOKEN**: Valid Salesforce access token |
| 79 | +- **ORG_ID**: Salesforce organization ID (15 or 18 characters) |
| 80 | +- **USER_ID**: Salesforce user ID (15 or 18 characters) |
| 81 | +- **METHOD**: HTTP method (default: GET) |
| 82 | +- **API_PATH**: API endpoint path (default: /accounts) |
| 83 | +- **--data**: JSON data for POST/PUT requests (as a string) |
| 84 | + |
| 85 | +### Examples |
| 86 | + |
27 | 87 | ```bash |
| 88 | +# Test the accounts endpoint |
| 89 | +./bin/invoke.py mycompany.my.salesforce.com TOKEN_123 00D123456789ABC 005123456789ABC |
| 90 | + |
| 91 | +# Test with POST data |
| 92 | +./bin/invoke.py mycompany.my.salesforce.com TOKEN_123 00D123456789ABC 005123456789ABC POST /unitofwork --data '{"data":{"accountName":"Test Account", "lastName":"Test", "subject":"Test Case"}}' |
| 93 | + |
| 94 | +# Test custom endpoint |
| 95 | +./bin/invoke.py mycompany.my.salesforce.com TOKEN_123 00D123456789ABC 005123456789ABC GET /health |
| 96 | +``` |
| 97 | + |
| 98 | +### Getting Salesforce Credentials |
| 99 | + |
| 100 | +To get the required Salesforce credentials for testing: |
| 101 | + |
| 102 | +1. **Access Token**: Use Salesforce CLI to generate a session token (`sf org display --target-org <alias> --json | jq .result.accessToken -r`). |
| 103 | +2. **Org ID**: Found in Setup → Company Information or by running `sf org display --target-org <alias> --json | jq .result.id -r`. |
| 104 | +3. **User ID**: Found in your user profile or Setup → Users or by running `sf org display --target-org <alias> --json | jq .result.userId -r`. |
| 105 | + |
| 106 | +## Running Automated Tests |
| 107 | + |
| 108 | +This project uses `pytest` for unit testing. To run the tests: |
| 109 | + |
| 110 | +```bash |
| 111 | +pytest |
| 112 | +``` |
| 113 | + |
| 114 | +## Manual Heroku Deployment |
| 115 | + |
| 116 | +### 1. Prerequisites |
| 117 | + |
| 118 | +- [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) installed |
| 119 | +- Git repository initialized |
| 120 | +- Heroku account with billing enabled (for add-ons) |
| 121 | + |
| 122 | +### 2. Create Heroku App |
| 123 | + |
| 124 | +```bash |
| 125 | +# Create a new Heroku app |
28 | 126 | heroku create your-app-name |
| 127 | + |
| 128 | +# Or let Heroku generate a name |
| 129 | +heroku create |
29 | 130 | ``` |
30 | 131 |
|
31 | | -3. Deploy to Heroku: |
| 132 | +### 3. Add Required Buildpacks |
| 133 | + |
| 134 | +The app requires two buildpacks in the correct order: |
| 135 | + |
32 | 136 | ```bash |
33 | | -git add . |
34 | | -git commit -m "Initial commit" |
35 | | -git push heroku main |
| 137 | +# Add the AppLink Service Mesh buildpack first |
| 138 | +heroku buildpacks:add heroku/heroku-applink-service-mesh |
| 139 | + |
| 140 | +# Add the Python buildpack second |
| 141 | +heroku buildpacks:add heroku/python |
36 | 142 | ``` |
37 | 143 |
|
38 | | -4. Configure Heroku AppLink: |
| 144 | +### 4. Provision the AppLink Add-on |
| 145 | + |
39 | 146 | ```bash |
| 147 | +# Provision the Heroku AppLink add-on |
40 | 148 | heroku addons:create heroku-applink |
| 149 | + |
| 150 | +# Set the required HEROKU_APP_ID config var |
| 151 | +heroku config:set HEROKU_APP_ID="$(heroku apps:info --json | jq -r '.app.id')" |
41 | 152 | ``` |
42 | 153 |
|
43 | | -5. Open your application: |
| 154 | +### 5. Deploy the Application |
| 155 | + |
44 | 156 | ```bash |
| 157 | +# Deploy to Heroku |
| 158 | +git push heroku main |
| 159 | + |
| 160 | +# Check deployment status |
| 161 | +heroku ps:scale web=1 |
45 | 162 | heroku open |
46 | 163 | ``` |
47 | 164 |
|
48 | | -## Endpoints |
| 165 | +### 6. Verify Deployment |
| 166 | + |
| 167 | +```bash |
| 168 | +# Check app logs |
| 169 | +heroku logs --tail |
| 170 | +``` |
| 171 | + |
| 172 | +## Heroku AppLink Setup |
| 173 | + |
| 174 | +(Instructions for `heroku salesforce:connect`, `heroku salesforce:authorizations:add`, and `heroku salesforce:publish` are identical to the Node.js version and can be referenced from the official documentation.) |
| 175 | + |
| 176 | +## Additional Resources |
| 177 | + |
| 178 | +### Documentation |
49 | 179 |
|
50 | | -- `GET /`: Returns a simple root page |
51 | | -- `GET /accounts`: Queries accounts using Heroku AppLink Data API |
| 180 | +- [Getting Started with Heroku AppLink](https://devcenter.heroku.com/articles/getting-started-heroku-applink) |
| 181 | +- [Heroku AppLink CLI Plugin](https://devcenter.heroku.com/articles/heroku-applink-cli) |
| 182 | +- [FastAPI Documentation](https://fastapi.tiangolo.com/) |
52 | 183 |
|
53 | | -## Note |
| 184 | +--- |
54 | 185 |
|
55 | | -This application requires proper Heroku AppLink configuration to work with the Data API. Make sure you have the necessary credentials and configuration set up in your environment. |
| 186 | +**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. |
0 commit comments