|
| 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 | +## Authentication |
| 11 | + |
| 12 | +All requests must be Authenticated. |
| 13 | + |
| 14 | +Authentication uses the Authorization header with a Personal Access Token (PAT) |
| 15 | +from the app. |
| 16 | + |
| 17 | +If you're using the http adaptor, set the `access_token` on the credential to |
| 18 | +your PAT. |
| 19 | + |
| 20 | +If you are using curl, add a the bearer token (in the example below the token |
| 21 | +will be expanded from an env var): |
| 22 | + |
| 23 | +``` |
| 24 | +curl -H "Authorization: Bearer $OPENFN_PAT" https://app.openfn.org/api/projects/<project-id>/workflows |
| 25 | +``` |
| 26 | + |
| 27 | +## REST API |
| 28 | + |
| 29 | +The Workflow API has the following RESTful structure: |
| 30 | + |
| 31 | +- `GET /api/projects/:projectId/workflows` - get a list of workflows for a |
| 32 | + project. Returns an array of Workflows. |
| 33 | +- `GET /api/projects/:projectId/workflows/:workflowId` - get a single workflow |
| 34 | + by id. Returns a single Workflow. |
| 35 | +- `PUT /api/projects/:projectId/workflows` - create a new Workflow. Include |
| 36 | + workflow JSON in the body. Returns the updated Workflow JSON. |
| 37 | +- `PUT /api/projects/:projectId/workflows/:workflowId` - update a Workflow. This |
| 38 | + will replace the existing workflow with the JSON in the body. Returns the |
| 39 | + updated Workflow JSON. |
| 40 | +- `PATCH /api/projects/:projectId/workflows/:workflowId` - partially update a |
| 41 | + Workflow. The existing workflow will be updated with the JSON in the body. |
| 42 | + |
| 43 | +## Workflow Structure |
| 44 | + |
| 45 | +A Workflow has the following structure: |
| 46 | + |
| 47 | +``` |
| 48 | +{ |
| 49 | + "name": "My Workflow", |
| 50 | + "id": "a414cb3b-e387-4c4f-b8de-70d51f1160da", |
| 51 | + "project_id": "79efba60-072a-4d4f-8d6c-22dfd3852176", |
| 52 | + "edges": [ |
| 53 | + { |
| 54 | + "condition_type": "always", |
| 55 | + "enabled": true, |
| 56 | + "source_job_id": null, |
| 57 | + "source_trigger_id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 58 | + "target_job_id": "26304a1e-267b-4bc9-940f-171db1905885", |
| 59 | + } |
| 60 | + ], |
| 61 | + "jobs": [ |
| 62 | + { |
| 63 | + "id": "26304a1e-267b-4bc9-940f-171db1905885", |
| 64 | + "body": "/* job code goes here */", |
| 65 | + "name": "my-job", |
| 66 | + "adaptor": "@openfn/language-common@latest", |
| 67 | + } |
| 68 | + ], |
| 69 | + "triggers": [ |
| 70 | + { |
| 71 | + "id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 72 | + "comment": null, |
| 73 | + "custom_path": null, |
| 74 | + "cron_expression": null, |
| 75 | + "type": "webhook", |
| 76 | + "enabled": true |
| 77 | + } |
| 78 | + ], |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +When creating a new Workflow, a UUID will be created by the server. |
| 83 | + |
| 84 | +When creating new steps or triggers (including when creating a whole new |
| 85 | +workflow), ids will be replaced by the server with UUIDs. |
| 86 | + |
| 87 | +You MUST ensure that all steps and triggers referenced by an edge are defined |
| 88 | +within the same workflow. |
| 89 | + |
| 90 | +## HTTP Adaptor Examples |
| 91 | + |
| 92 | +You will need to create a credential with `access_token` set to your Personal |
| 93 | +Access Token (PAT) and `baseUrl` set to your OpenFn instance (ie, |
| 94 | +`"https://app.openfn.org"`) |
| 95 | + |
| 96 | +Create new Workflow: |
| 97 | + |
| 98 | +``` |
| 99 | +post( |
| 100 | + `/api/projects/${$.projectId}/workflows`, |
| 101 | + { |
| 102 | + "name": "My Workflow", |
| 103 | + "edges": [ |
| 104 | + { |
| 105 | + "source_trigger_id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 106 | + "target_job_id": "26304a1e-267b-4bc9-940f-171db1905885", |
| 107 | + } |
| 108 | + ], |
| 109 | + "jobs": [ |
| 110 | + { |
| 111 | + "id": "26304a1e-267b-4bc9-940f-171db1905885", |
| 112 | + "body": "/* job code goes here */", |
| 113 | + "adaptor": "@openfn/language-common@latest", |
| 114 | + } |
| 115 | + ], |
| 116 | + "triggers": [ |
| 117 | + { |
| 118 | + "id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 119 | + "type": "webhook", |
| 120 | + "enabled": true |
| 121 | + } |
| 122 | + ], |
| 123 | + } |
| 124 | +) |
| 125 | +``` |
| 126 | + |
| 127 | +Add a new step to an existing Workflow: |
| 128 | + |
| 129 | +``` |
| 130 | +patch( |
| 131 | + `/api/projects/${$.projectId}/workflows/${$.workflowId}`, |
| 132 | + { |
| 133 | + "edges": [ |
| 134 | + { |
| 135 | + "source_job_id": "c79ce46c-ab0f-4f5b-bf2d-fed52aef2a41", |
| 136 | + "target_job_id": "new-job", |
| 137 | + } |
| 138 | + ], |
| 139 | + "jobs": [ |
| 140 | + { |
| 141 | + "id": "new-job", |
| 142 | + "body": "/* job code goes here */", |
| 143 | + "adaptor": "@openfn/language-common@latest", |
| 144 | + } |
| 145 | + ], |
| 146 | + } |
| 147 | +) |
| 148 | +``` |
0 commit comments