Discord API · error code

Discord error 4001: Unknown opcode

Permanent - do not retryGateway close codes (4000-4014)

Last verified against Discord Developer Docs - Opcodes and status codes

What Discord returns
You sent an invalid Gateway opcode or an invalid payload for an opcode. Don't do that!

What error 4001 means

Your bot sent a gateway payload whose op value is not one Discord accepts from clients, or whose d data did not match the expected shape for that opcode. Clients are only allowed to send a small set: 1 Heartbeat, 2 Identify, 3 Presence Update, 4 Voice State Update, 6 Resume, 8 Request Guild Members, and 31 Request Soundboard Sounds. Anything else, including the server-only opcodes (0 Dispatch, 7 Reconnect, 9 Invalid Session, 10 Hello, 11 Heartbeat ACK), is rejected with 4001.

If you use a mainstream library you will essentially never see this. It appears in custom gateway implementations, in bots that forward raw payloads (for example a voice library that sends voice-gateway opcodes to the main gateway), or when an op is accidentally sent as a string. It is not reconnect-safe in the sense that reconnecting without fixing the sender will produce it again, although Discord marks it as reconnectable.

What it looks like

// WebSocket close frame
code: 4001
reason: "Unknown opcode"

// a payload that would cause it
{ "op": 12, "d": {} }   // 12 is not a client-sendable opcode

Why it happens

  • Custom gateway client sending an opcode outside the client-allowed set
  • Voice gateway payloads sent over the main gateway socket
  • op serialized as a string or wrong field name (opcode instead of op)
  • Payload d object malformed for the opcode (for example Identify missing token or intents)
  • Sending a heartbeat with the wrong d type

How to fix Discord error 4001

  1. 1Log every outbound gateway payload and check op and d against the Gateway Events documentation
  2. 2Ensure op is an integer and the top-level fields are op, d, s, t
  3. 3Route voice payloads to the voice WebSocket, not the gateway
  4. 4If using a library, upgrade it; older versions may send payloads newer gateway versions reject

How to stop it recurring

Unless you are writing a library, do not hand-send gateway frames. If you are, build a typed encoder for the client-sendable opcodes and unit-test each payload shape against the documented schema.

The client-sendable opcode list is short by design; anything else belongs to Discord's side of the protocol. The gateway send budget those opcodes share is on the Discord limits page, and malformed-but-parseable frames fail one step earlier with 4002.

Official reference: Discord Developer Docs - Opcodes and status codes. See all Discord error codes or the Discord limits and quotas.

Related codes

Error 4001 - quick answers

What does Discord error 4001 mean?

Your bot sent a gateway payload whose op value is not one Discord accepts from clients, or whose d data did not match the expected shape for that opcode. Clients are only allowed to send a small set: 1 Heartbeat, 2 Identify, 3 Presence Update, 4 Voice State Update, 6 Resume, 8 Request Guild Members, and 31 Request Soundboard Sounds.

How do I fix Discord error 4001?

1. Log every outbound gateway payload and check op and d against the Gateway Events documentation 2. Ensure op is an integer and the top-level fields are op, d, s, t 3. Route voice payloads to the voice WebSocket, not the gateway 4. If using a library, upgrade it; older versions may send payloads newer gateway versions reject

Should I retry after error 4001?

No. Retrying the same request produces the same error; the condition has to be fixed first. Treat it as a permanent failure for that message and surface it, rather than looping.

Stop debugging Discord by hand

Connect the channel through Conferbot: tokens, webhooks and retries are handled, failures show as readable status.