AstralAPI Docs
Resources

Audit Log

Audit Log

Guild audit logs let you inspect moderation and configuration actions recorded for a server. These logs are essential for tracking changes, ensuring accountability, and maintaining server security in Astral, which is compatible with Discord's API structure. Audit logs capture a wide range of events, including guild updates, channel creations or deletions, member kicks or bans, role updates, and more.

Endpoints

Astral provides endpoints to retrieve and interact with audit logs. The primary endpoint allows you to fetch audit log entries for a specific guild. Below is a comprehensive table of available endpoints:

MethodRouteDescription
GET/guilds/\{guild.id\}/audit-logsRetrieves a list of audit log entries for the specified guild. You can filter results using query parameters to narrow down the entries based on criteria like time, user, or action type. This endpoint requires the VIEW_AUDIT_LOG permission for the bot or user making the request.

Ensure your bot has the necessary permissions to access audit logs. In Astral, this typically means the bot must have the VIEW_AUDIT_LOG scope granted via OAuth or explicit guild permissions.

Query Parameters

The GET endpoint supports several query parameters to filter and paginate results. These parameters help in efficiently retrieving relevant data without overwhelming the response.

ParameterTypeDescription
limitIntegerThe maximum number of audit log entries to return. The default is 50, and the maximum is 100 to prevent abuse.
beforeStringFetches entries before a specific audit log entry ID. This is useful for pagination.
afterStringFetches entries after a specific audit log entry ID. This is also for pagination.
user_idStringFilters entries by the ID of the user who performed the actions.
action_typeIntegerFilters entries by the type of action. For example, 22 might represent a message delete action. Refer to the action types section below for a full list.

The before and after parameters cannot be used together in the same request, as they represent opposite directions of pagination.

Audit Log Objects and Fields

Audit log responses contain structured objects that describe each logged event. The main object is an AuditLog object, which includes an array of AuditLogEntry objects. Below are the detailed fields for these objects.

AuditLog Object

The top-level response object for the GET endpoint.

FieldTypeDescription
entriesArray of AuditLogEntryAn array of audit log entries.
webhooksArray of WebhookAn array of webhooks involved in the actions (if any).
usersArray of UserAn array of users referenced in the entries, such as the actor or target.
integrationsArray of IntegrationAn array of integrations (e.g., bots or apps) involved.

AuditLogEntry Object

Each entry in the audit log represents a single action.

FieldTypeDescription
idStringThe unique ID of the audit log entry.
action_typeIntegerThe type of action performed. See the action types table below for details.
target_idStringThe ID of the target of the action (e.g., a channel, role, or user).
changesArray of ChangeAn array of changes made, if the action involved modifications (e.g., updates to a role).
optionsObjectAdditional options for the action, such as delete message days for a prune.
user_idStringThe ID of the user who performed the action.
reasonStringThe reason provided for the action, if any.
timestampString (ISO 8601)The time when the action occurred.

Change Object (within AuditLogEntry)

Represents individual changes in an action.

FieldTypeDescription
keyStringThe key of the changed field (e.g., "name" for a role name change).
old_valueAnyThe previous value of the field.
new_valueAnyThe new value of the field.

Action Types

Audit logs track various events. Here's a table of common action types, based on Astral's compatibility with Discord:

Action Type (Integer)Description
1Guild update.
10Channel create.
11Channel update.
12Channel delete.
20Member kick.
21Member prune.
22Member ban add.
23Member ban remove.
24Member update (e.g., role changes).
25Member disconnect (voice).
30Role create.
31Role update.
32Role delete.
40Invite create.
41Invite update.
42Invite delete.
50Webhook create.
51Webhook update.
52Webhook delete.
72Emoji create.
73Emoji update.
74Emoji delete.

Action types may evolve with platform updates. Always refer to the latest API documentation for the most accurate list.

JSON Examples

Below are examples of how to make requests and what responses might look like.

Request Example

For a GET request to fetch the last 50 audit logs filtered by a specific action type:

GET /guilds/1234567890/audit-logs?limit=50&action_type=22
Headers:
{
  "Authorization": "Bot YOUR_BOT_TOKEN"
}

Response Example

A successful response might look like this:

{
  "entries": [
    {
      "id": "9876543210",
      "action_type": 22,
      "target_id": "1122334455",
      "changes": [],
      "options": {
        "delete_member_days": "7",
        "count": 0
      },
      "user_id": "6677889900",
      "reason": "Violated community guidelines",
      "timestamp": "2023-10-01T12:00:00.000Z"
    }
  ],
  "webhooks": [],
  "users": [
    {
      "id": "6677889900",
      "username": "moderator_bot",
      "discriminator": "0001"
    }
  ],
  "integrations": []
}

Handle pagination in your application by using the before or after parameters based on the id from previous responses.

Rate Limits

This endpoint is subject to Astral's standard rate limits to prevent abuse. Specifically:

  • Global Limit: Up to 5 requests per 10 seconds per guild.
  • Burst Limit: You may burst up to 10 requests, but sustained usage will be throttled.
  • Headers: Responses include rate limit headers such as X-RateLimit-Limit, X-RateLimit-Remaining, and X-RateLimit-Reset for precise tracking.

If you exceed these limits, you will receive a 429 Too Many Requests response. Implement exponential backoff in your client to handle retries gracefully.

Rate limits are enforced per IP and per bot token. Monitor your usage to avoid disruptions.

Supplying an Audit Reason on Write Routes

Many moderation and management routes support the public header to provide a reason for actions, which will be recorded in the audit logs.

X-Audit-Log-Reason: Spam cleanup

Example

curl -X PUT \
  -H "Authorization: Bot $TOKEN" \
  -H "Content-Type: application/json" \
  -H "X-Audit-Log-Reason: Spam" \
  -d '{"delete_message_days":1}' \
  https://astraof.com/api/v1/guilds/<GUILD_ID>/bans/<USER_ID>

Use this header whenever your bot performs actions like banning users, deleting channels, or updating roles to ensure traceability.

Best Practices

To effectively use audit logs in your Astral applications, consider the following guidelines:

  • Efficient Querying: Use filters like user_id and action_type to minimize data transfer. Avoid fetching large unfiltered sets, as this can hit rate limits quickly.
  • Pagination Handling: Always implement pagination to handle large logs. Start with a reasonable limit (e.g., 50) and use before or after for subsequent requests.
  • Security and Privacy: Audit logs may contain sensitive information. Ensure that your application processes and stores this data securely, complying with relevant data protection regulations.
  • Logging Actions: When your bot performs actions, always include an X-Audit-Log-Reason header to provide context, making it easier for server administrators to review logs.
  • Error Handling: Check for permission errors (e.g., 403 Forbidden) if your bot lacks access. Also, handle 429 responses by implementing retry logic with increasing delays.
  • Monitoring Changes: Use audit logs to monitor for unusual activity, such as mass deletions or role changes, and integrate with alerting systems if needed.
  • Testing: In development, test your integration with a test guild to understand the response structures and avoid affecting live servers.

By following these practices, you can leverage audit logs to enhance moderation, improve accountability, and build more robust applications on Astral.

On this page

Astral API Docs | Audit Log