Scripts API

Overview

The Scripts API provides endpoints for creating, retrieving, updating, and deleting BattleScript code scripts. These endpoints allow users to manage their scripts, share them with others, and download them for use on BattleCore devices.

Script Endpoints

GET

/api/scripts

Description: Retrieves a list of scripts, with optional filtering and pagination.

Query Parameters:

  • page - Page number for pagination (default: 1)
  • limit - Number of scripts per page (default: 20)
  • sort - Sort field (e.g., "createdAt", "title")
  • order - Sort order ("asc" or "desc")
  • search - Search term for filtering by title or description
  • userId - Filter by creator user ID
  • tags - Filter by tags (comma-separated)

Response:

{
  "success": true,
  "data": {
    "scripts": [
      {
        "id": "string",
        "title": "string",
        "description": "string",
        "userId": "string",
        "username": "string",
        "tags": ["string"],
        "isPublic": boolean,
        "createdAt": "string",
        "updatedAt": "string",
        "likes": number,
        "downloads": number
      }
    ],
    "pagination": {
      "total": number,
      "page": number,
      "limit": number,
      "pages": number
    }
  }
}

Example:

// Get popular scripts
fetch('/api/scripts?sort=likes&order=desc&limit=10')
  .then(response => response.json())
  .then(data => {
    console.log('Popular scripts:', data.data.scripts);
  })
  .catch(error => console.error('Error:', error));
GET

/api/scripts/{id}

Description: Retrieves a specific script by ID.

Path Parameters:

  • id - The ID of the script to retrieve

Response:

{
  "success": true,
  "data": {
    "id": "string",
    "title": "string",
    "description": "string",
    "content": "string",
    "userId": "string",
    "username": "string",
    "tags": ["string"],
    "isPublic": boolean,
    "createdAt": "string",
    "updatedAt": "string",
    "likes": number,
    "downloads": number
  }
}

Example:

// Get a specific script
fetch('/api/scripts/abc123')
  .then(response => response.json())
  .then(data => {
    console.log('Script details:', data.data);
  })
  .catch(error => console.error('Error:', error));
POST

/api/scripts

Description: Creates a new script.

Headers:

{
  "Authorization": "Bearer {token}"
}

Request Body:

{
  "title": "string",
  "description": "string",
  "content": "string",
  "tags": ["string"],
  "isPublic": boolean
}

Response:

{
  "success": true,
  "message": "Script created successfully",
  "data": {
    "id": "string",
    "title": "string",
    "description": "string",
    "userId": "string",
    "tags": ["string"],
    "isPublic": boolean,
    "createdAt": "string",
    "updatedAt": "string"
  }
}

Example:

// Create a new script
const token = localStorage.getItem('authToken');

fetch('/api/scripts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    title: 'Capture the Flag Game Mode',
    description: 'A custom game mode for capture the flag',
    content: 'void startGame() => { /* game code here */ }',
    tags: ['game-mode', 'capture-the-flag'],
    isPublic: true
  })
})
.then(response => response.json())
.then(data => {
  console.log('Script created:', data.data);
})
.catch(error => console.error('Error:', error));
PUT

/api/scripts/{id}

Description: Updates an existing script.

Path Parameters:

  • id - The ID of the script to update

Headers:

{
  "Authorization": "Bearer {token}"
}

Request Body:

{
  "title": "string",
  "description": "string",
  "content": "string",
  "tags": ["string"],
  "isPublic": boolean
}

Response:

{
  "success": true,
  "message": "Script updated successfully",
  "data": {
    "id": "string",
    "title": "string",
    "description": "string",
    "userId": "string",
    "tags": ["string"],
    "isPublic": boolean,
    "updatedAt": "string"
  }
}

Example:

// Update a script
const token = localStorage.getItem('authToken');
const scriptId = 'abc123';

fetch(`/api/scripts/${scriptId}`, {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
  },
  body: JSON.stringify({
    title: 'Updated Capture the Flag Game Mode',
    description: 'An improved version of the CTF game mode',
    content: 'void startGame() => { /* updated game code here */ }',
    tags: ['game-mode', 'capture-the-flag', 'updated'],
    isPublic: true
  })
})
.then(response => response.json())
.then(data => {
  console.log('Script updated:', data.data);
})
.catch(error => console.error('Error:', error));
DELETE

/api/scripts/{id}

Description: Deletes a script.

Path Parameters:

  • id - The ID of the script to delete

Headers:

{
  "Authorization": "Bearer {token}"
}

Response:

{
  "success": true,
  "message": "Script deleted successfully"
}

Example:

// Delete a script
const token = localStorage.getItem('authToken');
const scriptId = 'abc123';

fetch(`/api/scripts/${scriptId}`, {
  method: 'DELETE',
  headers: {
    'Authorization': `Bearer ${token}`
  }
})
.then(response => response.json())
.then(data => {
  console.log('Script deleted:', data.message);
})
.catch(error => console.error('Error:', error));
GET

/api/scripts/{id}/download

Description: Downloads a script in a format suitable for BattleCore devices.

Path Parameters:

  • id - The ID of the script to download

Response:

Returns the script file as a download with appropriate headers.

Example:

// Download a script
const scriptId = 'abc123';

// Direct the browser to download the file
window.location.href = `/api/scripts/${scriptId}/download`;

// Alternatively, for programmatic access:
fetch(`/api/scripts/${scriptId}/download`)
  .then(response => response.blob())
  .then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = 'script.bs';
    document.body.appendChild(a);
    a.click();
    a.remove();
  })
  .catch(error => console.error('Error:', error));

Related Topics