Space Messages
Retrieve and save messages within a space conversation. These endpoints allow you to build custom chat interfaces and integrate with external systems.
Get Messages
Retrieve messages from a space conversation, optionally filtered by group.
Endpoint
GET /v1/workspaces/{workspaceId}/spaces/{spaceId}/messageAuthentication
This endpoint can be accessed with either a user space token or via the standard two-step authentication flow.
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
workspaceId | string (UUID) | Yes | The ID of your workspace |
spaceId | string (UUID) | Yes | The ID of the space |
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
groupId | string (UUID) | No | Filter messages by group conversation ID. If not provided, returns messages from the default group. |
Response
Success Response
Status Code: 200 OK
Body:
{
"messages": [
{
"id": "msg-uuid-1",
"type": "USER",
"message": "How do I get started with the API?",
"temporalUserId": "user-uuid-1",
"content": null,
"sources": null,
"sourcesMetadata": null,
"thumbUp": false,
"thumbDown": false,
"user": {
"id": "user-uuid-1",
"name": "John Doe",
"photo": "https://example.com/photo.jpg"
}
},
{
"id": "msg-uuid-2",
"type": "AI",
"message": "To get started with the API, first generate an access token...",
"temporalUserId": null,
"content": "Extended response content...",
"sources": ["https://docs.example.com/api"],
"sourcesMetadata": [{"title": "API Documentation", "url": "https://docs.example.com/api"}],
"thumbUp": true,
"thumbDown": false,
"user": null
}
]
}Response Fields
| Field | Type | Description |
|---|---|---|
messages | array | Array of message objects |
messages[].id | string (UUID) | Unique message identifier |
messages[].type | string | Message type: USER or AI |
messages[].message | string | The message content |
messages[].temporalUserId | string | null | ID of the user who sent the message |
messages[].content | string | null | Extended content (for AI responses) |
messages[].sources | array | null | Source URLs used for the response |
messages[].sourcesMetadata | array | null | Detailed source information |
messages[].thumbUp | boolean | Whether the message was upvoted |
messages[].thumbDown | boolean | Whether the message was downvoted |
messages[].user | object | null | User details (for USER type messages) |
Examples
Basic Request
# Get messages from a space
curl -X GET \
'https://api.sharely.ai/v1/workspaces/your-workspace-id/spaces/your-space-id/message?groupId=group-id' \
-H 'Authorization: Bearer YOUR_USER_SPACE_TOKEN'JavaScript Example
async function getMessages(spaceToken, spaceId, groupId = null) {
const url = new URL(
`${API_BASE_URL}/v1/workspaces/${WORKSPACE_ID}/spaces/${spaceId}/message`
);
if (groupId) {
url.searchParams.set('groupId', groupId);
}
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${spaceToken}`
}
});
if (!response.ok) {
throw new Error(`Failed to get messages: ${response.statusText}`);
}
return await response.json();
}
// Usage
const { messages } = await getMessages(userSpaceToken, spaceId, groupId);
messages.forEach(msg => {
console.log(`[${msg.type}] ${msg.message}`);
});Save Message
Save a new message to a space conversation. This is typically used by custom agent servers or external integrations.
Endpoint
POST /v1/workspaces/{workspaceId}/spaces/{spaceId}/messageAuthentication
This endpoint requires a user space token obtained through the activate-or-retrieve-user-space flow.
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
workspaceId | string (UUID) | Yes | The ID of your workspace |
spaceId | string (UUID) | Yes | The ID of the space |
Request Body
| Field | Type | Required | Description |
|---|---|---|---|
newMessage | string | Yes | The message content |
groupId | string (UUID) | Yes | The group conversation ID |
type | string | Yes | Message type: USER or AI |
messageAIId | string | No | ID to associate AI responses with user messages |
languageId | string | No | Language code for the message (e.g., en, es) |
rag | object | No | RAG context data (for AI messages with sources) |
Request Body Details
RAG Object Structure
{
"rag": {
"knowledge": [
{
"metadata": {
"text": "Relevant content from knowledge base...",
"source": "https://docs.example.com/page"
}
}
]
}
}Response
Success Response
Status Code: 200 OK
Body:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"type": "USER",
"message": "How do I configure authentication?",
"createdAt": "2025-01-15T10:30:00Z"
}Examples
Save User Message
curl -X POST \
'https://api.sharely.ai/v1/workspaces/your-workspace-id/spaces/your-space-id/message' \
-H 'Authorization: Bearer YOUR_USER_SPACE_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"newMessage": "How do I configure authentication?",
"groupId": "group-id",
"type": "USER"
}'Save AI Response with Sources
curl -X POST \
'https://api.sharely.ai/v1/workspaces/your-workspace-id/spaces/your-space-id/message' \
-H 'Authorization: Bearer YOUR_USER_SPACE_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"newMessage": "To configure authentication, follow these steps...",
"groupId": "group-id",
"type": "AI",
"messageAIId": "user-message-id",
"rag": {
"knowledge": [
{
"metadata": {
"text": "Authentication requires...",
"source": "https://docs.example.com/auth"
}
}
]
}
}'JavaScript Example
async function saveMessage(spaceToken, spaceId, options) {
const response = await fetch(
`${API_BASE_URL}/v1/workspaces/${WORKSPACE_ID}/spaces/${spaceId}/message`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${spaceToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
newMessage: options.message,
groupId: options.groupId,
type: options.type,
messageAIId: options.replyToId,
languageId: options.language,
rag: options.rag
})
}
);
if (!response.ok) {
throw new Error(`Failed to save message: ${response.statusText}`);
}
return await response.json();
}
// Save a user message
const userMessage = await saveMessage(userSpaceToken, spaceId, {
message: 'What are the API rate limits?',
groupId: groupId,
type: 'USER'
});
// Save an AI response
const aiResponse = await saveMessage(userSpaceToken, spaceId, {
message: 'The API has the following rate limits...',
groupId: groupId,
type: 'AI',
replyToId: userMessage.id,
rag: {
knowledge: [
{
metadata: {
text: 'Rate limits documentation...',
source: 'https://docs.example.com/rate-limits'
}
}
]
}
});Building a Custom Chat Integration
class SharelyChat {
constructor(workspaceId, spaceId, spaceToken) {
this.workspaceId = workspaceId;
this.spaceId = spaceId;
this.spaceToken = spaceToken;
this.groupId = null;
}
async initialize(groupId) {
this.groupId = groupId;
return await this.getMessages();
}
async getMessages() {
const response = await fetch(
`${API_BASE_URL}/v1/workspaces/${this.workspaceId}/spaces/${this.spaceId}/message?groupId=${this.groupId}`,
{
headers: {
'Authorization': `Bearer ${this.spaceToken}`
}
}
);
return await response.json();
}
async sendUserMessage(message) {
return await this.saveMessage(message, 'USER');
}
async sendAIResponse(message, replyToId, sources = []) {
return await this.saveMessage(message, 'AI', {
messageAIId: replyToId,
rag: sources.length > 0 ? {
knowledge: sources.map(s => ({
metadata: { text: s.text, source: s.url }
}))
} : undefined
});
}
async saveMessage(message, type, options = {}) {
const response = await fetch(
`${API_BASE_URL}/v1/workspaces/${this.workspaceId}/spaces/${this.spaceId}/message`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${this.spaceToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
newMessage: message,
groupId: this.groupId,
type,
...options
})
}
);
return await response.json();
}
}
// Usage
const chat = new SharelyChat(workspaceId, spaceId, userSpaceToken);
await chat.initialize(groupId);
const userMsg = await chat.sendUserMessage('How do I reset my password?');
const aiReply = await chat.sendAIResponse(
'To reset your password, go to Settings > Security...',
userMsg.id,
[{ text: 'Password reset instructions...', url: 'https://docs.example.com/password' }]
);Error Responses
404 Not Found
Space not found:
{
"error": "Error",
"message": "Space not found"
}Group not found:
{
"error": "Error",
"message": "Group not found"
}400 Bad Request
The space is private:
{
"error": "Error",
"message": "The space is private"
}Related Endpoints
- Get Space - Get space details
- Create Group Conversation - Create a new conversation group
- RAG Search - Search knowledge for AI responses