#1145: Embedding and Authorizing a Web Widget in Your Own Application
Отредактирована: 46 дней назадIntegrating an external system and quick login
Please read the overview of the integration logic described in the article first (https://support.swarmica.com/article/en/557.html).
IMPORTANT: This document provides examples of OTP_CODE_KEY and API key.
When going to production, you will need to change the cipher (OTP_CODE_KEY) and API key used for integration.
System setup (once/as needed)
Setting up the system, both Swarmica and the external system being integrated (your application), is done once (or reconfigured as needed).
On the Swarmica side:
- On the server: You need to set up a secret key for the cipher and the maximum allowed time deviation in the timestamp and connect the authorization provider code: To do this, set the variables in
/root/swarmica/.env:
root@swarmica# grep -i code .env
AUTH_PROVIDERS=email,code
OTP_CODE_KEY=E655633B-2FAB-4661-9637-EXAMPLE
OTP_CODE_TIMESTAMP_DELTA=120
The parameter OTP_CODE_TIMESTAMP_DELTA is the lifespan of the generated link for user authorization in seconds. For example and testing, this parameter is set to 120 seconds; for production, it is recommended to set it to a few seconds (5-10).
The parameter OTP_CODE_KEY is a secret key used for encrypting/decrypting authorization data. After completing the development, it is recommended to replace the key.
Restart the system:
cd /root/swarmica
docker compose down
docker compose up -d
- In the Swarmica app (Administrator rights required): Create an API key that will be used by the backend of the integrated system in Settings > API Integration. It will look like this:
EXAMPLE-cdc6-4be4-a31d-1cf0aa129b99
On the side of the integrated system (billing or panel):
- Prepare the code to work with the Swarmica API.
- Write down in configurable variables:
- Code for the cipher (same as OTP_CODE_KEY above): E655633B-2FAB-4661-9637-EXAMPLE
- API key:
EXAMPLE-cdc6-4be4-a31d-1cf0aa129b99 - Template HTML code for embedding the widget with OTP settings https://support.swarmica.com/article/en/599.html
Example widget code:
<!-- start embedded Swarmica widget script -->
<iframe src="https://support.yourdomain.tld/widgets/index.html?id=veNilW0QFQhTq2hg&locale=en&otpdata=URLENCODED_ДАННЫЕ_СМ_ДАЛЕЕ" id="veNilW0QFQhTq2hg" style="position: fixed; right: 12px; bottom: 12px; width: 56px; height: 56px; overflow: hidden; "
allowTransparency="true"
frameborder="0"
allowTransparency="true" frameborder="0"></iframe>
<script src="https://support.yourdomain.tld/widgets/render.js"></script>
<script>
const iframe = document.getElementById("veNilW0QFQhTq2hg")
iframe.onload = () => {
renderWidget("veNilW0QFQhTq2hg")
}
</script>
<!-- end embedded Swarmica widget scripts -->
Receiving a code for the user (when registering / authorizing the user in your application)
When a user signs in to your application, its backend must check whether a code exists. If it does not, the backend must request a code for the user:
https://support.yourdomain.tld/api/schema/doc/#post-/api/auth/otp/code/request
Here, `support.yourdomain.tld` is the address of your Swarmica installation.
For convenience, the request can include not only the user's email address but also other fields. If an account with these details does not exist, Swarmica automatically creates the user and issues a code. The parameters of the `user` object are identical to the Create user API call. See also User creation parameters below.
IMPORTANT: This API request can only be made by an account with Administrator permissions. You need the API token created during system setup.
Example request:
curl -X POST "https://support.yourdomain.tld/api/auth/otp/code/request"
-H "accept: application/json"
-H "authorization: Token EXAMPLE-cdc6-4be4-a31d-1cf0aa129b99"
-H "content-type: application/json"
-d '{"user":{"name":"Joe Doe","email":"joedoe@swarmica.tld","role":"CUSTOMER","ext_id":"fake_user_id","locale":"en","custom_fields":[{"uid":"OGRcFb1U8XFHM2r1","value":"freelancer"},{"uid":"IHZAwFQD3bBJSmAM","value":"+1234567890"}]}}'
System response:
{
"code": "EXAMPLE-76fd-4797-a61e-658aeda5876b",
"user": {
"uid": "6zndSQzdagK8WLfK",
"name": "Joe Doe",
"email": "joedoe@swarmica.tld",
"date_joined": "2025-06-18T07:17:30.564278Z",
"role": "CUSTOMER",
"image_path": null,
"signatures": null,
"ext_id": "fake_user_id",
"custom_fields": [],
"schedule": null,
"locale": "en",
"tz_offset_minutes": 0,
"timezone": "UTC",
"organization": "s5cN_OBc_U5MXosr",
"source": "api",
"kcs_role": "CANDIDATE",
"created_at": "2025-06-18T07:17:30.857456Z",
"updated_at": "2025-06-18T07:17:30.857463Z",
"groups": [],
"is_system": false,
"is_employee": false,
"anonymous": false,
"identities": [
{
"uid": "8ipu8ZpttthVGJIa",
"user": "6zndSQzdagK8WLfK",
"source": "EMAIL",
"ext_id": "joedoe@swarmica.tld",
"ext_id_type": null,
"data": null,
"confirmed": true,
"default": true
}
],
"merged_into": null,
"last_login": null
}
}
It is useful to save the user's UID, and you must save the code. The UID makes it easier to match accounts in other integrations later; the code is required to generate the `otpdata` used to authorize the widget.
Generating the `otpdata` parameter for widget authorization
When you need to create a login link, for example when the user clicks My requests, your application's backend must:
- Create JSON data in the following format:
`{"email": "joedoe@swarmica.com", "code": "EXAMPLE-76fd-4797-a61e-658aeda5876b", "timestamp": int(time.time())}` - Encrypt it using the encryption algorithm by calling the `encrypt` method: https://support.yourdomain.tld//api/schema/doc/#post-/api/auth/otp/code/encrypt
- Insert the resulting value into the `otpdata` parameter in URL-encoded form in the widget initialization code. For example, if the `encrypt` function returns:
GPQi9F26yw/MaNUWvmSYrShqK+n9IdtcbUraB5/WfNNdjEJqZbWk1Qifn1BF3iV4phT0tt5GtH51Ks6DMCz6hZk4NxfNnHnfpCrM+P0y8BlTK2v6nGjN+KoP/1x1OptMN+at2ZNTlPn7nVrmxfMeJdu7pruLTDo62snxHQymWiPLKvhOdgCfFAKHyWWwmxIQ
The link must then contain:
&otpdata=GPQi9F26yw%2FMaNUWvmSYrShqK%2Bn9IdtcbUraB5%2FWfNNdjEJqZbWk1Qifn1BF3iV4phT0tt5GtH51Ks6DMCz6hZk4NxfNnHnfpCrM%2BP0y8BlTK2v6nGjN%2BKoP%2F1x1OptMN%2Bat2ZNTlPn7nVrmxfMeJdu7pruLTDo62snxHQymWiPLKvhOdgCfFAKHyWWwmxIQ
When testing from the client side, it is recommended to open the widget page source in another browser or in an incognito window, or after signing out of Swarmica if you have previously signed in as a privileged user.
User creation parameters
Expand
When creating a user through https://support.yourdomain.tld/api/schema/doc/#post-/api/auth/otp/code/request or directly through https://support.yourdomain.tld/api/schema/doc/#post-/api/users/, you can provide the following data:
```json
{
"user": {
"name": "Joe Doe",
"email": "joedoe@swarmica.tld",
"role": "CUSTOMER",
"ext_id": "fake_user_id",
"locale": "en",
"custom_fields": [
{"uid": "OGRcFb1U8XFHM2r1", "value": "freelancer"},
{"uid": "IHZAwFQD3bBJSmAM", "value": "+1234567890"}
]
}
}
```
Required fields:
name — the user's name, string.
email — the user's email address, used to identify the user.
role — the user's role in Swarmica. For customers, this can be `CUSTOMER` (customer privileges; can work only with their own requests/chats) or `CUSTOMER_ADMIN` (customer privileges; can also work with requests/chats of other users in their Organization).
Optional fields:
ext_id — a string containing the user's ID in an external system, such as a Solar customer portal. It can be used to match accounts and search through the API and interface.
locale — the Swarmica interface language for this user (`ru` or `en`).
custom_fields — an array of custom-field values. You can view all user custom fields through the custom_fields API with the filter `model = user`.
Custom-field values:
Custom-field values are provided as an object:
```json
{
"uid": "CUSTOM_FIELD_UID",
"value": "CUSTOM_FIELD_VALUE_IN_ITS_FIELD_TYPE_FORMAT"
}
```
For example, the value for a Phone field (UID = `IHZAwFQD3bBJSmAM`) is a text value.
For a Customer type field (UID = `OGRcFb1U8XFHM2r1`), provide the key from the `choices` dictionary corresponding to the required option:
```json
{
"uid": "OGRcFb1U8XFHM2r1",
"name": {
"en": "User type",
"ru": "Тип пользователя"
},
"field_type": "DROPDOWN",
"applicable_to": [
"user"
],
"mandatory_for": [],
"permissions": "INTERNAL",
"default_value": null,
"properties": {
"choices": {
"customer": {
"en": "Customer",
"ru": "Заказчик"
},
"freelancer": {
"en": "Freelancer",
"ru": "Фрилансер"
}
}
}
}
```
For the Freelancer value, specify `freelancer`; for the Customer value, specify `customer`.
All values and keys are configured through the graphical interface.