This document provides comprehensive documentation for the Code Sharing Platform API, including GraphQL endpoints, WebSocket connections, and REST endpoints.
All authenticated endpoints require a JWT token in the Authorization header:
Authorization: Bearer <token>
mutation Login($email: String!, $password: String!) {
login(email: $email, password: $password) {
token
user {
id
username
email
}
}
}mutation Register($username: String!, $email: String!, $password: String!) {
register(username: $username, email: $email, password: $password) {
token
user {
id
username
email
}
}
}query GetSnippets($limit: Int, $offset: Int) {
snippets(limit: $limit, offset: $offset) {
id
title
description
language
author {
id
username
}
tags
views
createdAt
}
}query GetSnippet($id: String!) {
snippet(id: $id) {
id
title
description
code
language
author {
id
username
}
tags
comments {
id
content
author {
username
}
}
createdAt
updatedAt
}
}mutation CreateSnippet($title: String!, $description: String, $code: String!, $language: String!, $tags: [String!]) {
createSnippet(title: $title, description: $description, code: $code, language: $language, tags: $tags) {
id
title
url
}
}mutation UpdateSnippet($id: String!, $title: String, $description: String, $code: String, $tags: [String!]) {
updateSnippet(id: $id, title: $title, description: $description, code: $code, tags: $tags) {
id
title
updatedAt
}
}mutation DeleteSnippet($id: String!) {
deleteSnippet(id: $id)
}query GetComments($snippetId: String!) {
comments(snippetId: $snippetId) {
id
content
author {
id
username
}
createdAt
replies {
id
content
author {
username
}
}
}
}mutation CreateComment($snippetId: String!, $content: String!, $parentId: String) {
createComment(snippetId: $snippetId, content: $content, parentId: $parentId) {
id
content
author {
username
}
createdAt
}
}Connect to /ws endpoint for real-time code updates:
const ws = new WebSocket('ws://localhost:8080/ws?token=<jwt_token>');
// Subscribe to snippet updates
ws.send(JSON.stringify({
action: 'SUBSCRIBE',
snippetId: '123'
}));
// Send code update
ws.send(JSON.stringify({
action: 'UPDATE_CODE',
snippetId: '123',
code: '...',
userId: 'user-id'
}));
// Listen for updates
ws.onmessage = (event) => {
const message = JSON.parse(event.data);
// Handle message
};All errors follow this format:
{
"timestamp": "2024-01-01T00:00:00Z",
"status": 400,
"error": "Bad Request",
"message": "Error message details",
"path": "/api/endpoint"
}- Rate limit: 100 requests per minute per IP
- Rate limit headers:
X-RateLimit-Limit: Total requests allowedX-RateLimit-Remaining: Remaining requestsX-RateLimit-Reset: Reset time in Unix timestamp