This documentation describes how to interact with the Document API endpoints using tRPC.
document.create- Required: Yes
- Type: Bearer token authentication
{
title: string,
content: any // Can be any valid JSON data
}import { createTRPCProxyClient } from '@trpc/client';
import type { AppRouter } from './path-to-your-router';
// Initialize the tRPC client
const trpc = createTRPCProxyClient<AppRouter>({
url: 'YOUR_API_URL',
headers: {
Authorization: 'Bearer YOUR_AUTH_TOKEN'
}
});
// Create a new document
try {
const newDocument = await trpc.document.create.mutate({
title: "My New Document",
content: {
// Your document content here
text: "Hello World",
// Add any other fields as needed
}
});
console.log('Document created:', newDocument);
} catch (error) {
console.error('Error creating document:', error);
}On success, the API will return the created document object:
{
id: string;
title: string;
content: any;
createdAt: Date;
updatedAt: Date;
// ... other document fields
}The API may return the following errors:
-
400 Bad Request
- When title or content is missing
{ code: 'BAD_REQUEST', message: 'Invalid input: title and content are required' }
-
401 Unauthorized
- When authentication token is missing or invalid
-
500 Internal Server Error
- When there's a server-side error processing the request
-
Error Handling
- Always implement proper error handling in your client code
- Check for specific error codes and messages to provide appropriate feedback to users
-
Content Structure
- While the content field accepts any valid JSON data, maintain a consistent structure for your documents
- Consider implementing a schema for your content based on your application's needs
-
Authentication
- Store authentication tokens securely
- Implement token refresh mechanisms if required
- Never expose tokens in client-side code or version control
document.delete- Required: Yes
- Type: Bearer token authentication
{
id: string // The ID of the document to delete
}try {
const result = await trpc.document.delete.mutate({
id: "document-id-here"
});
console.log('Document deleted:', result);
} catch (error) {
console.error('Error deleting document:', error);
}On success, the API will return:
{
success: true,
id: string // The ID of the deleted document
}-
404 Not Found
- When the document doesn't exist or user doesn't have access
{ code: 'NOT_FOUND', message: 'Document not found or you do not have permission to delete it' }
-
401 Unauthorized
- When authentication token is missing or invalid
-
500 Internal Server Error
- When there's a server-side error processing the request
-
Access Control
- Only users with access to the document can delete it
- The API performs authorization checks before deletion
- Deletion is permanent and cannot be undone
-
Cascading Deletion
- Deleting a document will also delete all associated versions and data
- Ensure users understand the implications of document deletion
- Implement appropriate rate limiting in your client code
- Consider implementing retry mechanisms with exponential backoff for failed requests
- Cache document data where appropriate to reduce API calls