import { CodeGroup } from '../code.tsx'
import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from '../md.tsx'
# Chat App API
For versatile conversational apps using a Q&A format, call the chat-messages API to initiate dialogue. Maintain ongoing conversations by passing the returned conversation_id. Response parameters and templates depend on LangGenius Prompt Eng. settings. **[Before you start, READ This !! What is a Bearer Token](https://swagger.io/docs/specification/authentication/bearer-authentication/)**
### Authentication
Service API of Dify authenticates using an `API-Key`.
It is suggested that developers store the `API-Key` in the backend instead of sharing or storing it in the client side to avoid the leakage of the `API-Key`, which may lead to property loss.
All API requests should include your `API-Key` in the **`Authorization`** HTTP Header, as shown below:
```javascript
Authorization: Bearer {API_KEY}
```
---
Create a new conversation message or continue an existing dialogue.
### Request Body
(Optional) Provide user input fields as key-value pairs, corresponding to variables in Prompt Eng. Key is the variable name, Value is the parameter value. If the field type is Select, the submitted Value must be one of the preset choices.
{!!props.variables.length && props.variables.map(
val => (
{val.name ? `${val.name}` : ''}
)
)}
User input/question content
- Blocking type, waiting for execution to complete and returning results. (Requests may be interrupted if the process is long)
- streaming returns. Implementation of streaming return based on SSE (**[Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)**).
(Required) Conversation ID: ‼️ leave it empty for first-time (eg. conversation_id: "") conversation ‼️; pass conversation_id from context to continue dialogue.
The user identifier, defined by the developer, must ensure uniqueness within the app.
Uploaded files.
- `type` file type: currently only `image` is supported.
- `transfer_method` transfer method:
- `remote_url`: Image address.
- `local_file`: upload file.
- `upload_file_id` Upload file ID. Required when `transfer_method` is `local_file`.
- `url` image address. Required when `transfer_method` is `remote_url`.
```bash {{ title: 'cURL' }}
curl --location --request POST '${props.appDetail.api_base_url}/chat-messages' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
"inputs": {},
"query": "eh",
"response_mode": "streaming",
"conversation_id": "1c7e55fb-1ba2-4e10-81b5-30addcea2276",
"user": "abc-123"
}'
```
### blocking
```json {{ title: 'Response' }}
{
"answer": "Hi, is there anything I can help you?",
"conversation_id": "45701982-8118-4bc5-8e9b-64562b4555f2",
"created_at": 1679587005,
"id": "059f87d9-15c0-473a-870c-fde95cdcc1e4"
}
```
### streaming
```streaming {{ title: 'Response' }}
data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
data: {"id": "5ad4cb98-f0c7-4085-b384-88c403be6290", "answer": " I", "created_at": 1679586595}
```
---
Rate received messages on behalf of end-users with likes or dislikes. This data is visible in the Logs & Annotations page and used for future model fine-tuning.
### Path Params
Message ID
### Request Body
like or dislike, null is undo
The user identifier, defined by the developer, must ensure uniqueness within the app.
```bash {{ title: 'cURL' }}
curl --location --request POST '${props.appDetail.api_base_url}/messages/{message_id}/feedbacks' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
"rating": "like",
"user": "abc-123"
}'
```
```json {{ title: 'Response' }}
{
"has_more": false,
"data": [
{
"id": "WAz8eIbvDR60rouK",
"conversation_id": "xgQQXg3hrtjh7AvZ",
"inputs": {},
"query": "...",
"answer": "...",
"feedback": "like",
"created_at": 692233200
},
{
"id": "hSIhXBhNe8X1d8Et"
// ...
}
]
}
```
---
Get next questions suggestions for the current message
### Path Params
Message ID
```bash {{ title: 'cURL' }}
curl --location --request GET '${props.appDetail.api_base_url}/messages/{message_id}/suggested' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--header 'Content-Type: application/json' \
```
```json {{ title: 'Response' }}
{
"result": "success",
"data": [
"a",
"b",
"c"
]
]
}
```
---
The first page returns the latest `limit` bar, which is in reverse order. Load previous pages by passing the `first_id` of the last message on the current page to the `first_id` parameter of the next request.
### Query
Conversation ID
ID of the first chat record on the current page. The default is none.
How many chats are returned in one request
The user identifier, defined by the developer, must ensure uniqueness within the app.
```bash {{ title: 'cURL' }}
curl --location --request GET '${props.appDetail.api_base_url}/messages?user=abc-123&conversation_id='
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
```
```json {{ title: 'Response' }}
{
"has_more": false,
"data": [
{
"id": "WAz8eIbvDR60rouK",
"username": "FrankMcCallister",
"phone_number": "1-800-759-3000",
"avatar_url": "https://assets.protocol.chat/avatars/frank.jpg",
"display_name": null,
"conversation_id": "xgQQXg3hrtjh7AvZ",
"created_at": 692233200
},
{
"id": "hSIhXBhNe8X1d8Et"
// ...
}
]
}
```
---
Gets the session list of the current user. By default, the last 20 sessions are returned.
### Query
The ID of the last record on the current page, default none.
"How many chats are returned in one request
The user identifier, defined by the developer, must ensure uniqueness within the app.
```bash {{ title: 'cURL' }}
curl --location --request GET '${props.appDetail.api_base_url}/conversations?user=abc-123&last_id=&limit=20' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
```
```json {{ title: 'Response' }}
{
"limit": 20,
"has_more": false,
"data": [
{
"id": "10799fb8-64f7-4296-bbf7-b42bfbe0ae54",
"name": "New chat",
"inputs": {
"book": "book",
"myName": "Lucy"
},
"status": "normal",
"created_at": 1679667915
},
{
"id": "hSIhXBhNe8X1d8Et"
// ...
}
]
}
```
---
Rename conversations; the name is displayed in multi-session client interfaces.
### Request Body
New name
The user identifier, defined by the developer, must ensure uniqueness within the app.
```bash {{ title: 'cURL' }}
curl --location --request POST '${props.appDetail.api_base_url}/conversations/{converation_id}/name' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--data-raw '{
"name": "",
"user": "abc-123"
}'
```
```json {{ title: 'Response' }}
{
"result": "success"
}
```
---
Delete conversation.
### Request Body
The user identifier, defined by the developer, must ensure uniqueness within the app.
```bash {{ title: 'cURL' }}
curl --location --request DELETE '${props.appDetail.api_base_url}/conversations/{convsation_id}' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--data '{
"user": "abc-123"
}'
```
```json {{ title: 'Response' }}
{
"result": "success"
}
```
---
Upload files to the server for text generation and dialogue. Uploaded files are only available to the current end user.
### Request Body
The request method is `multipart/form-data`
The file to upload. Currently supports png, jpg, jpeg, webp, gif format files.
User ID, rules are defined by the developer, and it is necessary to ensure that the user ID is unique within the application.
```bash {{ title: 'cURL' }}
curl --location --request POST '${props.appDetail.api_base_url}/files/upload' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--form 'file=@"/path/to/file"'
```
```json {{ title: 'Response' }}
{
"id": "72fa9618-8f89-4a37-9b33-7e1178a24a67",
"name": "example.png",
"size": 1024,
"extension": "png",
"mime_type": "image/png",
"created_by": 123,
"created_at": 1577836800,
}
```
---
Speech to text, only supports openai model.
### Request Body
Audio file.
File uploads are currently limited to 15 MB and the following input file types are supported: mp3, mp4, mpeg, mpga, m4a, wav, and webm.
```bash {{ title: 'cURL' }}
curl --location --request POST '${props.appDetail.api_base_url}/conversations/name' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY' \
--form 'file=@localfile;type=audio/mp3'
```
```json {{ title: 'Response' }}
{
"text": ""
}
```
---
Content include:
1. Retrieve configured Input parameters, including variable names, field names, types, and default values. Typically used for displaying these fields in a form or filling in default values after the client loads.
2. Configuration of uploading images, including whether to enable image uploading, the number of uploaded images and the uploading method. Note: This configuration is only available when using a model that supports multimodality.
### Query
The user identifier, defined by the developer, must ensure uniqueness within the app.
```bash {{ title: 'cURL' }}
curl --location --request GET '${props.appDetail.api_base_url}/parameters?user=abc-123' \
--header 'Authorization: Bearer ENTER-YOUR-SECRET-KEY'
```
```json {{ title: 'Response' }}
{
"introduction": "nice to meet you",
"user_input_form": [
{
"text-input": {
"label": "a",
"variable": "a",
"required": true,
"max_length": 48,
"default": ""
}
}
{
// ...
}
],
"file_upload": {
"image": {
"enabled": true,
"number_limits": 3,
"transfer_methods": [
"remote_url",
"local_file"
]
}
}
}
```