Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/betterdiscord/betterdiscord/llms.txt

Use this file to discover all available pages before exploring further.

BdApi.Net provides a custom fetch implementation for making HTTP requests with additional options not available in the standard browser fetch API.

fetch

Makes an HTTP request with extended options for timeouts, redirects, and SSL certificate validation.
BdApi.Net.fetch(
  input: string | URL | Request,
  init?: RequestInit & {
    timeout?: number,
    maxRedirects?: number,
    rejectUnauthorized?: boolean
  }
): Promise<Response>
input
string | URL | Request
required
The resource to fetch. Can be a URL string, URL object, or Request object.
init
RequestInit
Standard fetch options plus additional BetterDiscord-specific options
init.method
string
default:"GET"
HTTP method. Supported: GET, PUT, POST, DELETE, PATCH, OPTIONS, HEAD, CONNECT, TRACE
init.headers
object
Request headers as key-value pairs
init.body
string | FormData | ReadableStream
Request body data
init.redirect
string
default:"follow"
Redirect mode: "follow", "error", or "manual"
init.signal
AbortSignal
AbortSignal for canceling the request
init.keepalive
boolean
Whether to keep the connection alive

Extended options

init.timeout
number
default:"3000"
Request timeout in milliseconds. Request will be aborted after this time.
init.maxRedirects
number
default:"20"
Maximum number of redirects to follow before failing
init.rejectUnauthorized
boolean
default:"true"
Whether to reject requests with invalid SSL certificates. Set to false to allow self-signed certificates.
Returns a Promise that resolves to a standard Response object.

Usage examples

Basic GET request

const response = await BdApi.Net.fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);

POST request with JSON body

const response = await BdApi.Net.fetch('https://api.example.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'John Doe',
    email: 'john@example.com'
  })
});

const result = await response.json();
console.log('User created:', result);

Request with custom timeout

try {
  const response = await BdApi.Net.fetch('https://api.example.com/slow-endpoint', {
    timeout: 10000 // 10 seconds
  });
  const data = await response.json();
} catch (error) {
  if (error.name === 'AbortError') {
    console.error('Request timed out');
  }
}

Request with custom headers

const response = await BdApi.Net.fetch('https://api.example.com/protected', {
  headers: {
    'Authorization': 'Bearer token123',
    'X-Custom-Header': 'value'
  }
});

Handling redirects

const response = await BdApi.Net.fetch('https://example.com/redirect', {
  redirect: 'manual',
  maxRedirects: 5
});

if (response.redirected) {
  console.log('Redirected to:', response.url);
}

Allowing self-signed certificates

// Useful for local development or testing
const response = await BdApi.Net.fetch('https://localhost:8080/api', {
  rejectUnauthorized: false
});

Aborting a request

const controller = new AbortController();

// Cancel after 5 seconds
setTimeout(() => controller.abort(), 5000);

try {
  const response = await BdApi.Net.fetch('https://api.example.com/data', {
    signal: controller.signal
  });
  const data = await response.json();
} catch (error) {
  if (error.name === 'AbortError') {
    console.log('Request was cancelled');
  }
}

Downloading a file

const response = await BdApi.Net.fetch('https://example.com/file.zip', {
  timeout: 30000 // 30 seconds for large file
});

if (response.ok) {
  const blob = await response.blob();
  const url = URL.createObjectURL(blob);
  
  const a = document.createElement('a');
  a.href = url;
  a.download = 'file.zip';
  a.click();
  
  URL.revokeObjectURL(url);
}

Checking response status

const response = await BdApi.Net.fetch('https://api.example.com/data');

if (response.ok) {
  const data = await response.json();
  console.log('Success:', data);
} else if (response.status === 404) {
  console.error('Resource not found');
} else if (response.status === 500) {
  console.error('Server error');
} else {
  console.error('Request failed:', response.status, response.statusText);
}

Using with async/await error handling

module.exports = class MyPlugin {
  async fetchUserData(userId) {
    try {
      const response = await BdApi.Net.fetch(
        `https://api.example.com/users/${userId}`,
        {
          timeout: 5000,
          headers: {
            'Accept': 'application/json'
          }
        }
      );
      
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }
      
      return await response.json();
    } catch (error) {
      BdApi.UI.showToast(`Failed to fetch user: ${error.message}`, {
        type: 'error'
      });
      return null;
    }
  }
};

Notes

  • The default timeout is 3 seconds (3000ms). Adjust this for slower endpoints.
  • The default max redirects is 20. This prevents infinite redirect loops.
  • SSL certificate validation is enabled by default. Only disable rejectUnauthorized when necessary and understand the security implications.
  • This implementation uses the native Node.js fetch under the hood, providing more control than the browser’s fetch API.
  • All standard Response methods are available: json(), text(), blob(), arrayBuffer(), etc.