|
| 1 | +.. _integrations_rest_api: |
| 2 | + |
| 3 | +REST API Integration |
| 4 | +==================== |
| 5 | + |
| 6 | +DejaCode offers a REST API to allow integration with external applications in a |
| 7 | +generic way. You can use it to fetch, create, and update DejaCode Requests |
| 8 | +from your own scripts or applications. |
| 9 | + |
| 10 | +The full REST API documentation is also available in the DejaCode web UI under |
| 11 | +**Tools > API Documentation**. |
| 12 | + |
| 13 | +This guide focuses specifically on interacting with **DejaCode Requests**. |
| 14 | + |
| 15 | +.. note:: |
| 16 | + |
| 17 | + Example HTTP requests assume the DejaCode URL is ``https://localhost``. |
| 18 | + Replace with your actual instance URL. |
| 19 | + |
| 20 | +Prerequisites |
| 21 | +------------- |
| 22 | + |
| 23 | +- A **DejaCode API Key**, available from your **Profile** settings page. |
| 24 | + |
| 25 | +Authentication |
| 26 | +-------------- |
| 27 | + |
| 28 | +Include your **API Key** in the "Authorization" HTTP header for every request. |
| 29 | +The key must be prefixed by the string literal ``Token`` followed by a space: |
| 30 | + |
| 31 | + Authorization: Token abcdef123456 |
| 32 | + |
| 33 | +.. warning:: |
| 34 | + Treat your API key like a password — keep it secret and secure. |
| 35 | + |
| 36 | +Example using cURL:: |
| 37 | + |
| 38 | + curl -X GET \ |
| 39 | + https://localhost/api/v2/requests/ \ |
| 40 | + -H "Authorization: Token abcdef123456" |
| 41 | + |
| 42 | +Example using Python:: |
| 43 | + |
| 44 | + import requests |
| 45 | + |
| 46 | + api_url = "https://localhost/api/v2/requests/" |
| 47 | + headers = {"Authorization": "Token abcdef123456"} |
| 48 | + params = {"page": "2"} |
| 49 | + response = requests.get(api_url, headers=headers, params=params) |
| 50 | + print(response.json()) |
| 51 | + |
| 52 | +Request List |
| 53 | +------------ |
| 54 | + |
| 55 | +**Endpoint:** ``GET /api/v2/requests/`` |
| 56 | + |
| 57 | +This endpoint lists all requests. Responses include pagination fields ``next`` |
| 58 | +and ``previous`` to navigate through pages. |
| 59 | + |
| 60 | +You can sort the list using ``?ordering=FIELD``. Prefix a field with ``-`` to |
| 61 | +reverse the sort order (descending). Available fields: |
| 62 | + |
| 63 | +- ``title`` |
| 64 | +- ``request_template`` |
| 65 | +- ``status`` |
| 66 | +- ``priority`` |
| 67 | +- ``assignee`` |
| 68 | +- ``requester`` |
| 69 | +- ``created_date`` |
| 70 | +- ``last_modified_date`` |
| 71 | + |
| 72 | +Filtering is supported using ``FIELD=VALUE`` syntax. Available filters include: |
| 73 | + |
| 74 | +- ``request_template`` |
| 75 | +- ``status`` |
| 76 | +- ``requester`` |
| 77 | +- ``assignee`` |
| 78 | +- ``priority`` |
| 79 | +- ``content_type`` |
| 80 | +- ``last_modified_date`` |
| 81 | + |
| 82 | +Example: Get closed requests sorted by last modification date:: |
| 83 | + |
| 84 | + api_url = "https://localhost/api/v2/requests/" |
| 85 | + headers = {"Authorization": "Token abcdef123456"} |
| 86 | + params = {"status": "closed", "ordering": "last_modified_date"} |
| 87 | + response = requests.get(api_url, headers=headers, params=params) |
| 88 | + print(response.json()) |
| 89 | + |
| 90 | +Request Details |
| 91 | +--------------- |
| 92 | + |
| 93 | +**Endpoint:** ``GET /api/v2/requests/{uuid}/`` |
| 94 | + |
| 95 | +Returns all available information for a specific request. Replace ``{uuid}`` |
| 96 | +with the UUID of the request you want to retrieve. |
| 97 | + |
| 98 | +Example JSON response snippet:: |
| 99 | + |
| 100 | + { |
| 101 | + "uuid": "adf1835e-4b58-42d0-b1f4-c57791167d19", |
| 102 | + "title": "Issue title", |
| 103 | + "request_template": "https://localhost/api/v2/request_templates/5b106292-d8b6-459c-abda-e6a87527a0db/", |
| 104 | + "status": "open", |
| 105 | + "assignee": "username", |
| 106 | + "notes": "", |
| 107 | + "serialized_data": {"Notes": "This version has a known vulnerability."}, |
| 108 | + "created_date": "2025-08-12T17:41:47.424373+04:00", |
| 109 | + "last_modified_date": "2025-08-12T17:42:29.031833+04:00", |
| 110 | + "comments": [ |
| 111 | + { |
| 112 | + "uuid": "8ee73eb2-353a-4e84-8536-fe4e25a1abf6", |
| 113 | + "username": "username", |
| 114 | + "text": "Comment content.", |
| 115 | + "created_date": "2025-08-14T09:17:55.397285+04:00" |
| 116 | + } |
| 117 | + ] |
| 118 | + } |
| 119 | + |
| 120 | +Create a Request |
| 121 | +---------------- |
| 122 | + |
| 123 | +**Endpoint:** ``POST /api/v2/requests/`` |
| 124 | + |
| 125 | +Required fields: |
| 126 | + |
| 127 | +- **title** (string): A short, descriptive title of the request. |
| 128 | +- **request_template** (string): URI of the template to use. |
| 129 | + |
| 130 | +Optional fields: |
| 131 | + |
| 132 | +- **status** (string): ``open``, ``closed``, or ``draft``. Default is ``open``. |
| 133 | +- **assignee** (string): Username of the person assigned. |
| 134 | +- **priority** (string|null): Priority level. |
| 135 | +- **product_context** (string|null): URI of a product context. |
| 136 | +- **notes** (string): Notes related to the request. |
| 137 | +- **serialized_data** (string): Additional structured data. |
| 138 | +- **is_private** (boolean): True if only visible to requester/reviewers. |
| 139 | +- **content_object** (string|null): URI of associated content object. |
| 140 | +- **cc_emails** (array of strings): List of emails to notify. |
| 141 | + |
| 142 | +.. note:: |
| 143 | + |
| 144 | + The structure of **serialized_data** depends on the "Request Template" used |
| 145 | + for the request. To help construct valid **serialized_data**, consult the |
| 146 | + ``form_data_layout`` field available in the Request Template list at |
| 147 | + ``https://localhost/api/v2/request_templates/``. |
| 148 | + |
| 149 | +Example of minimal JSON payload:: |
| 150 | + |
| 151 | + { |
| 152 | + "title": "New vulnerability found", |
| 153 | + "request_template": "Address Vulnerabilities in Product Packages" |
| 154 | + } |
| 155 | + |
| 156 | +Example using cURL:: |
| 157 | + |
| 158 | + api_url="https://localhost/api/v2/requests/" |
| 159 | + headers="Authorization: Token abcdef123456" |
| 160 | + data='{ |
| 161 | + "title": "New vulnerability found", |
| 162 | + "request_template": "Address Vulnerabilities in Product Packages" |
| 163 | + }' |
| 164 | + |
| 165 | + curl -X POST "$api_url" -H "$headers" -d "$data" |
| 166 | + |
| 167 | +Example using Python:: |
| 168 | + |
| 169 | + import requests |
| 170 | + api_url = "https://localhost/api/v2/requests/" |
| 171 | + headers = { |
| 172 | + "Authorization": "Token abcdef123456", |
| 173 | + "Content-Type": "application/json" |
| 174 | + } |
| 175 | + data = { |
| 176 | + "title": "New vulnerability found", |
| 177 | + "request_template": "Address Vulnerabilities in Product Packages", |
| 178 | + "assignee": "username" |
| 179 | + } |
| 180 | + response = requests.post(api_url, headers=headers, json=data) |
| 181 | + print(response.json()) |
| 182 | + |
| 183 | +Update a Request |
| 184 | +---------------- |
| 185 | + |
| 186 | +**Endpoint:** ``PUT /api/v2/requests/{uuid}/`` |
| 187 | + |
| 188 | +Performs a full update. All fields of the request must be provided. |
| 189 | + |
| 190 | +Partial Update |
| 191 | +-------------- |
| 192 | + |
| 193 | +**Endpoint:** ``PATCH /api/v2/requests/{uuid}/`` |
| 194 | + |
| 195 | +Allows updating only specific fields. For example, to close a request:: |
| 196 | + |
| 197 | + import requests |
| 198 | + api_url = "https://localhost/api/v2/requests/{uuid}/" |
| 199 | + headers = { |
| 200 | + "Authorization": "Token abcdef123456", |
| 201 | + "Content-Type": "application/json" |
| 202 | + } |
| 203 | + data = {"status": "closed"} |
| 204 | + response = requests.patch(api_url, headers=headers, json=data) |
| 205 | + print(response.json()) |
| 206 | + |
| 207 | +Add comment |
| 208 | +----------- |
| 209 | + |
| 210 | +``POST /api/v2/requests/{uuid}/add_comment/`` |
| 211 | + |
| 212 | +This endpoint allows you to attach a new comment to an existing request. |
| 213 | +A successful call will store the comment and return a confirmation message. |
| 214 | + |
| 215 | +**Payload example**: |
| 216 | + |
| 217 | +.. code-block:: json |
| 218 | +
|
| 219 | + { |
| 220 | + "text": "Comment content" |
| 221 | + } |
| 222 | +
|
| 223 | +**Notes**: |
| 224 | + - The ``uuid`` in the URL must correspond to the target request. |
| 225 | + - The ``text`` field is required and should contain the full comment content. |
| 226 | + - Comments are attributed to the authenticated user making the request. |
| 227 | + - A successful request returns HTTP 201 with a status message. |
| 228 | + - Invalid or missing fields will return HTTP 400 along with error details. |
0 commit comments