|
| 1 | +--- |
| 2 | +title: Workflows API |
| 3 | +sidebar_label: Workflows API |
| 4 | +--- |
| 5 | + |
| 6 | +The Workflows API allows you to programmatically create and modify Workflows. |
| 7 | + |
| 8 | +You can use the Workflows API with the `http` adaptor or curl. |
| 9 | + |
| 10 | +:::info Version Compatibility |
| 11 | + |
| 12 | +The Workflows API was introduced in version 2.10.10, January 2025 |
| 13 | + |
| 14 | +::: |
| 15 | + |
| 16 | +## Authentication |
| 17 | + |
| 18 | +All requests must be Authenticated. |
| 19 | + |
| 20 | +Authentication uses the Authorization header with a Personal Access Token (PAT) |
| 21 | +from the app. |
| 22 | + |
| 23 | +If you're using the http adaptor, set the `access_token` on the credential to |
| 24 | +your PAT. |
| 25 | + |
| 26 | +If you are using curl, add the bearer token (in the example below the token |
| 27 | +will be expanded from an env var): |
| 28 | + |
| 29 | +``` |
| 30 | +curl -H "Authorization: Bearer $OPENFN_PAT" https://app.openfn.org/api/projects/<project-id>/workflows |
| 31 | +``` |
| 32 | + |
| 33 | +## REST API |
| 34 | + |
| 35 | +The Workflow API has the following RESTful structure: |
| 36 | + |
| 37 | +- `GET /api/projects/:projectId/workflows` - get a list of workflows for a |
| 38 | + project. Returns an array of Workflows. |
| 39 | +- `GET /api/projects/:projectId/workflows/:workflowId` - get a single workflow |
| 40 | + by id. Returns a single Workflow. |
| 41 | +- `PUT /api/projects/:projectId/workflows` - create a new Workflow. Include |
| 42 | + workflow JSON in the body. Returns the updated Workflow JSON. |
| 43 | +- `PUT /api/projects/:projectId/workflows/:workflowId` - update a Workflow. This |
| 44 | + will replace the existing workflow with the JSON in the body. Returns the |
| 45 | + updated Workflow JSON. |
| 46 | +- `PATCH /api/projects/:projectId/workflows/:workflowId` - partially update a |
| 47 | + Workflow. The existing workflow will be updated with the JSON in the body. |
| 48 | + |
| 49 | +## Workflow Structure |
| 50 | + |
| 51 | +A Workflow has the following structure: |
| 52 | + |
| 53 | +``` |
| 54 | +{ |
| 55 | + "name": "My Workflow", |
| 56 | + "id": "a414cb3b-e387-4c4f-b8de-70d51f1160da", |
| 57 | + "project_id": "79efba60-072a-4d4f-8d6c-22dfd3852176", |
| 58 | + "edges": [ |
| 59 | + { |
| 60 | + "id": "759fe475-ed23-4914-8a6d-155968bc0aa1" |
| 61 | + "condition_type": "always", |
| 62 | + "enabled": true, |
| 63 | + "source_job_id": null, |
| 64 | + "source_trigger_id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 65 | + "target_job_id": "26304a1e-267b-4bc9-940f-171db1905885", |
| 66 | + } |
| 67 | + ], |
| 68 | + "jobs": [ |
| 69 | + { |
| 70 | + "id": "26304a1e-267b-4bc9-940f-171db1905885", |
| 71 | + "body": "/* job code goes here */", |
| 72 | + "name": "my-job", |
| 73 | + "adaptor": "@openfn/language-common@latest", |
| 74 | + } |
| 75 | + ], |
| 76 | + "triggers": [ |
| 77 | + { |
| 78 | + "id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 79 | + "comment": null, |
| 80 | + "custom_path": null, |
| 81 | + "cron_expression": null, |
| 82 | + "type": "webhook", |
| 83 | + "enabled": true |
| 84 | + } |
| 85 | + ], |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +When creating a new Workflow, the server will generate UUIDs for the workflow |
| 90 | +and all steps and edges. You can use any id string you like in the creation of |
| 91 | +new nodes and edges - so long as id usage is consistent. |
| 92 | + |
| 93 | +When matching a PUT or PATCH request, new steps and edges MUST be assigned |
| 94 | +UUIDs. If using the `http` adaptor, you can use `util.uuid()` for this (see the |
| 95 | +example below). |
| 96 | + |
| 97 | +You MUST ensure that any steps and triggers referenced by an edge are defined |
| 98 | +within the same workflow. |
| 99 | + |
| 100 | +## HTTP Adaptor Examples |
| 101 | + |
| 102 | +You will need to create a credential with `access_token` set to your Personal |
| 103 | +Access Token (PAT) and `baseUrl` set to your OpenFn instance (ie, |
| 104 | +`"https://app.openfn.org"`) |
| 105 | + |
| 106 | +Create new Workflow: |
| 107 | + |
| 108 | +```js |
| 109 | +post(`/api/projects/${$.projectId}/workflows`, { |
| 110 | + body: { |
| 111 | + name: 'My Workflow', |
| 112 | + edges: [ |
| 113 | + { |
| 114 | + source_trigger_id: 'trigger-1', |
| 115 | + target_job_id: 'job-1', |
| 116 | + condition_type: 'always', |
| 117 | + }, |
| 118 | + ], |
| 119 | + jobs: [ |
| 120 | + { |
| 121 | + id: 'job-1', |
| 122 | + name: 'My Job', |
| 123 | + body: '/* job code goes here */', |
| 124 | + adaptor: '@openfn/language-common@latest', |
| 125 | + }, |
| 126 | + ], |
| 127 | + triggers: [ |
| 128 | + { |
| 129 | + id: 'trigger-1', |
| 130 | + type: 'webhook', |
| 131 | + enabled: true, |
| 132 | + }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + headers: { 'content-type': 'application/json' }, |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +The resulting workflow with updated UUIDs and metadata will be written to |
| 140 | +`state.data.workflow`. |
| 141 | + |
| 142 | +Add a new step and edge to an existing Workflow: |
| 143 | + |
| 144 | +```js |
| 145 | +fn(state => { |
| 146 | + const jobId = util.uuid(); |
| 147 | + state.diff = { |
| 148 | + edges: [ |
| 149 | + { |
| 150 | + id: util.uuid(), |
| 151 | + source_job_id: 'c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41', |
| 152 | + target_job_id: jobId, |
| 153 | + condition_type: 'always', |
| 154 | + }, |
| 155 | + ], |
| 156 | + jobs: [ |
| 157 | + { |
| 158 | + id: jobId, |
| 159 | + body: '/* job code goes here */', |
| 160 | + adaptor: '@openfn/language-common@latest', |
| 161 | + }, |
| 162 | + ], |
| 163 | + }; |
| 164 | + return state; |
| 165 | +}); |
| 166 | +patch(`/api/projects/${$.projectId}/workflows/${$.workflowId}`, { |
| 167 | + body: $.diff, |
| 168 | + headers: { 'content-type': 'application/json' }, |
| 169 | +}); |
| 170 | +``` |
| 171 | + |
| 172 | +The resulting workflow will be written to `state.data.workflow`. |
0 commit comments