Documentation
← Back to Integrations
JavaScript/TypeScript Integration
Use Flux Relay in any JavaScript or TypeScript project with our official SDK
Installation
bun add @postacksolutions/flux-relay-sdkBasic Usage
Basic Example
import { FluxRelay } from '@postacksolutions/flux-relay-sdk';
// Initialize the client
const client = new FluxRelay({
apiUrl: 'https://flux.postacksolutions.com/api',
serverId: 'your-server-id',
apiKey: 'your-api-key',
});
// Authenticate a user
const auth = await client.authenticateUser({
externalUserId: 'user_123',
username: 'john_doe',
metadata: {
email: 'john@example.com',
avatar: 'https://example.com/avatar.jpg',
},
});
console.log('Authenticated:', auth.user);
console.log('Access Token:', auth.accessToken);Working with Conversations
Conversations Example
// Get all conversations
const conversations = await client.getConversations();
console.log('Conversations:', conversations);
// Create a new conversation
const conversation = await client.createConversation({
participantIds: ['user_456'],
name: 'Chat with Jane',
isGroup: false,
});
console.log('Created conversation:', conversation);Sending and Receiving Messages
Messages Example
// Get messages from a conversation
const messages = await client.getMessages(conversationId, {
limit: 50,
before: '2024-01-01T00:00:00Z', // Optional: get messages before this date
});
console.log('Messages:', messages);
// Send a message
const message = await client.sendMessage(conversationId, {
content: 'Hello, world!',
type: 'text',
});
console.log('Sent message:', message);File Upload
File Upload Example
// Upload a file
const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const uploadResult = await client.uploadFile(file);
console.log('File uploaded:', uploadResult);
// { fileId: 'file_...', url: 'https://...' }
// Send a message with attachment
await client.sendMessage(conversationId, {
content: 'Check out this file!',
type: 'text',
attachments: [{
id: uploadResult.fileId,
filename: file.name,
mimeType: file.type,
size: file.size,
}],
});Error Handling
Error Handling Example
import { FluxRelay, FluxRelayError } from '@postacksolutions/flux-relay-sdk';
try {
const client = new FluxRelay({
apiUrl: 'https://flux.postacksolutions.com/api',
serverId: 'your-server-id',
apiKey: 'your-api-key',
});
const auth = await client.authenticateUser({
externalUserId: 'user_123',
username: 'john_doe',
});
} catch (error) {
if (error instanceof FluxRelayError) {
console.error('Flux Relay Error:', error.message);
console.error('Status Code:', error.statusCode);
console.error('Error Code:', error.errorCode);
} else {
console.error('Unknown error:', error);
}
}API Reference
For complete API documentation, see the API Reference.
The SDK provides TypeScript types for all methods and responses. Check your IDE's autocomplete for available methods and parameters.