Get your API key on your My Account page and use it to authorize your requests.
Your API key provides access to your connected data sources via Chat Ur Data. Be careful with it.
Interacting with Chat Ur Data via API
To chat via API, you'll need to use a combination of two endpoints:
a POST to create a thread or add a message to a thread
a GET to retrieve the messages in the thread
Start a New Thread
To start a new thread you'll post a message with no thread_id to the new chat endpoint. The response will include a thread_id that you can poll on for updates to the thread, as well as a run status so you know if it's finished yet.
import requests
import json
# Set up variables
create_chat_url = "https://www.chaturdata.com/api/newMessage"
api_key = "your_api_key_here" # Replace with your actual access token
message = "your_message_here" # Replace with the message you want to send
# Headers
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
# Data payload
new_conversation = {
"message": message
}
# Making the API call
response = requests.post(create_chat_url, headers=headers, json=new_conversation)
# Check for successful response
if response.status_code == 200:
response_data = response.json()
print("API Response:", response_data)
# Example of using the response data
if 'thread_id' in response_data:
thread_id = response_data['thread_id']
# Here you can trigger a function to poll on that thread_id
else:
print(f"Create Chat Failed with status code {response.status_code}: {response.text}")
const axios = require('axios');
// Set up variables
const createChatUrl = 'https://www.chaturdata.com/api/newMessage';
const apiKey = 'your_api_key_here'; // Replace with your actual access token
const message = 'your_message_here'; // Replace with the message you want to send
// Headers
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
};
// Data payload
const newConversation = {
message: message,
};
// Making the API call
axios.post(createChatUrl, newConversation, { headers: headers })
.then(response => {
console.log('API Response:', response.data);
// Example of using the response data
if ('thread_id' in response.data) {
const threadId = response.data.thread_id;
// Here you can trigger a function to poll on that thread_id
}
})
.catch(error => {
console.error(`Create Chat Failed with status code ${error.response.status}: ${error.response.data}`);
});
Get the Messages in your Thread
To get the messages in a thread you need to call the getThread endpoint.
import requests
# Set up the necessary variables
url = "https://www.chaturdata.com/api/getThread"
api_key = "api_key" # Replace with your actual api_key
thread_id = "your_thread_id_here" # Replace with your actual thread ID
# Headers and parameters for the GET request
headers = {
"Authorization": f"Bearer {access_token}"
}
params = {
"threadId": thread_id,
}
# Perform the GET request
response = requests.get(url, headers=headers, params=params)
# Check for successful response
if response.status_code == 200:
# Process the response data
data = response.json()
if 'messages' in data:
for message in data['messages']:
for content in message.get('content', []):
if content['type'] == 'text':
print(content['text']['value'])
else:
print(f"Failed to fetch messages, status code: {response.status_code}: {response.text}")
const axios = require('axios');
// Set up the necessary variables
const url = 'https://www.chaturdata.com/api/getThread';
const apiKey = 'api_key'; // Replace with your actual api_key
const threadId = 'your_thread_id_here'; // Replace with your actual thread ID
// Headers and parameters for the GET request
const headers = {
'Authorization': `Bearer ${apiKey}`,
};
const params = {
threadId: threadId,
};
// Perform the GET request
axios.get(url, { headers: headers, params: params })
.then(response => {
// Process the response data
const data = response.data;
if ('messages' in data) {
data.messages.forEach(message => {
message.content.forEach(content => {
if (content.type === 'text') {
console.log(content.text.value);
}
});
});
}
})
.catch(error => {
console.error(`Failed to fetch messages, status code: ${error.response.status}: ${error.response.data}`);
});
Because threads can take time to process, you will probably need to poll on this endpoint.
We recommend 3-5 second intervals. Here's the logic for polling:
const status = chatResponse.data.run_status.status;
// polling until one of these two happens:
if (status === "completed") {
// boom, thread's ready, stop polling
} else if (["expired", "cancelled", "failed"].includes(status)) {
// stop polling... error message will be in the response
}
Add a Message to an Existing Thread
Use the same endpoint but include the thread_id to add a message to your thread.
import requests
import json
# Set up variables
create_chat_url = "https://www.chaturdata.com/api/newMessage"
api_key = "your_api_key_here" # Replace with your actual access token
message = "your_message_here" # Replace with the message you want to send
thread_id = "thread uuid" # this is the thread_id returned by this endpoint previously
# Headers
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
# Data payload
new_conversation = {
"message": message,
"thread_id": thread_id
}
# Making the API call
response = requests.post(create_chat_url, headers=headers, json=new_conversation)
# Check for successful response
if response.status_code == 200:
response_data = response.json()
print("API Response:", response_data)
# Example of using the response data
if 'thread_id' in response_data:
thread_id = response_data['thread_id']
# Here you can trigger a function to poll on that thread_id
else:
print(f"Create Chat Failed with status code {response.status_code}: {response.text}")
const axios = require('axios');
// Set up variables
const createChatUrl = 'https://www.chaturdata.com/api/newMessage';
const apiKey = 'your_api_key_here'; // Replace with your actual access token
const message = 'your_message_here'; // Replace with the message you want to send
const threadId = 'thread uuid'; // This is the thread_id returned by this endpoint previously
// Headers
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
};
// Data payload
const newConversation = {
message: message,
thread_id: threadId,
};
// Making the API call
axios.post(createChatUrl, newConversation, { headers: headers })
.then(response => {
console.log('API Response:', response.data);
// Example of using the response data
if ('thread_id' in response.data) {
const threadId = response.data.thread_id;
// Here you can trigger a function to poll on that thread_id
}
})
.catch(error => {
console.error(`Create Chat Failed with status code ${error.response && error.response.status}: ${error.response && error.response.data}`);
});