Opcodes and Status Codes

Gateway

All Gateway events in Discord are tagged with an opcode that denotes the payload type. Your connection to the Gateway may also sometimes close. When it does, you will receive a close code that tells you what happened.

Gateway Opcodes
CodeNameActionDescription
0DispatchReceiveAn event was dispatched.
1HeartbeatSend/ReceiveKeep the WebSocket connection alive.
2IdentifySendStart a new session during the initial handshake.
3Presence UpdateSendUpdate the client's presence.
4Voice State UpdateSendJoin/leave or move between voice channels and calls.
5Voice Server PingSendPing the Discord voice servers.
6ResumeSendResume a previous session that was disconnected.
7ReconnectReceiveYou should attempt to reconnect and resume immediately.
8Request Guild MembersSendRequest information about guild members.
9Invalid SessionReceiveThe session has been invalidated. You should reconnect and identify/resume accordingly.
10HelloReceiveSent immediately after connecting, contains the heartbeat_interval to use.
11Heartbeat ACKReceiveAcknowledge a received heartbeat.
12Guild SyncSendRequest all members and presences for guilds.
13Call ConnectSendRequest a private channels's pre-existing call data.
14Guild SubscriptionsSendUpdate subscriptions for a guild.
15Lobby ConnectSendJoin a lobby.
16Lobby DisconnectSendLeave a lobby.
17Lobby Voice StatesSendUpdate the client's voice state in a lobby.
18Stream CreateSendCreate a stream for the client.
19Stream DeleteSendEnd a client stream.
20Stream WatchSendWatch a user's stream.
21Stream PingSendPing a user stream's voice server.
22Stream Set PausedSendPause/resume a client stream.
23LFG SubscriptionsSendUpdate subscriptions for an LFG lobby.
24Request Guild Application CommandsSendRequest guild application commands.
25Embedded Activity CreateSendLaunch an embedded activity in a voice channel or call.
26Embedded Activity DeleteSendStop an embedded activity.
27Embedded Activity UpdateSendUpdate an embedded activity.
28Request Forum UnreadsSendRequest forum channel unread counts.
29Remote CommandSendSend a remote command to an embedded (Xbox, PlayStation) voice session.
30Request Deleted Entity IDsSendRequest deleted entity IDs not matching a given hash for a guild.
31Request Soundboard SoundsSendRequest soundboard sounds for guilds.
32Speed Test CreateSendCreate a voice speed test.
33Speed Test DeleteSendDelete a voice speed test.
34Request Last MessagesSendRequest last messages for a guild's channels.
35Search Recent MembersSendRequest information about recently-joined guild members.
36Request Channel StatusesSendRequest voice channel statuses for a guild.
Gateway Close Event Codes
CodeDescriptionExplanation
4000Unknown errorWe're not sure what went wrong. Try reconnecting?
4001Unknown opcodeYou sent an invalid Gateway opcode or an invalid payload for an opcode. Don't do that!
4002Decode errorYou sent an invalid payload. Don't do that!
4003Not authenticatedYou sent us a payload prior to identifying, or this session has been invalidated.
4004Authentication failedThe account token sent with your identify payload is incorrect.
4005Already authenticatedYou sent more than one identify payload. Don't do that!
4006Session no longer validYour session is no longer valid.
4007Invalid seqThe sequence sent when resuming the session was invalid. Reconnect and start a new session.
4008Rate limitedWoah nelly! You're sending payloads too quickly. Slow it down! You will be disconnected on receiving this.
4009Session timed outYour session timed out. Reconnect and start a new one.
4010Invalid shardYou sent us an invalid shard when identifying.
4011Sharding requiredThe session would have handled too many guilds - you are required to shard your connection in order to connect.
4012Invalid API versionYou sent an invalid version for the Gateway.
4013Invalid intent(s)You sent an invalid intent for a Gateway Intent. You may have incorrectly calculated the bitwise value.
4014Disallowed intent(s)You sent a disallowed intent for a Gateway Intent. You may have tried to specify an intent that you have not enabled or are not approved for.

Voice

The Voice Gateway has its own set of opcodes and close codes.

Voice Opcodes
CodeNameActionDescription
0IdentifySendStart a new voice WebSocket connection.
1Select ProtocolSendSelect the voice protocol.
2ReadyReceiveComplete the WebSocket handshake.
3HeartbeatSend/ReceiveKeep the WebSocket connection alive.
4Session DescriptionReceiveDescribe the session.
5SpeakingSend/ReceiveIndicate which users are speaking.
6Heartbeat ACKReceiveAcknowledge a received heartbeat.
7ResumeSendResume a previous session that was disconnected.
8HelloReceiveSent immediately after connecting, contains the heartbeat_interval to use.
9ResumedReceiveResponse to acknowledging a successful resume.
11Client ConnectReceiveIndicate that clients have connected to the voice channel.
12VideoSend/ReceiveDescribe the video session.
13Client DisconnectserverIndicate that a client has disconnected from the voice channel.
14Session UpdateSend/ReceiveIndicate an update in session description.
15Media Sink WantsSend/ReceiveIndicate the media streams wanted for simulcasting.
16Voice Backend VersionSend/ReceiveVersion information about the voice backend.
17Channel Options UpdateReceiveIndicate an update in the RTC log ephemeral key.
18Client FlagsReceiveIndicate a client's flags.
19Speed TestSendStart a speed test.
20Client PlatformReceiveIndicate the platform a client is connected on
Voice Close Event Codes
CodeDescriptionExplanation
4001Unknown opcodeYou sent an invalid opcode.
4002Failed to decode payloadYou sent a invalid payload in your identifying to the Gateway.
4003Not authenticatedYou sent a payload before identifying with the Gateway.
4004Authentication failedThe token you sent in your identify payload is incorrect.
4005Already authenticatedYou sent more than one identify payload. Stahp.
4006Session no longer validYour session is no longer valid.
4009Session timeoutYour session has timed out.
4011Server not foundWe can't find the server you're trying to connect to.
4012Unknown protocolWe didn't recognize the protocol you sent.
4014DisconnectedChannel was deleted, you were kicked, voice server changed, or the main Gateway session was dropped. Should not reconnect.
4015Voice server crashedThe server crashed. Our bad! Try resuming.
4016Unknown encryption modeWe didn't recognize your encryption.

HTTP

The API will return semantically valid HTTP response codes based on the success of your request. The following table can be used as a reference for response codes it will return.

HTTP Response Codes
CodeMeaning
200 (OK)The request completed successfully.
201 (CREATED)The entity was created successfully.
202 (ACCEPTED)The request was accepted but the resource is not ready. A retry_after key should be present in the body.
204 (NO CONTENT)The request completed successfully but returned no content.
304 (NOT MODIFIED)The entity was not modified (no action was taken).
400 (BAD REQUEST)The request was improperly formatted, or the server couldn't understand it.
401 (UNAUTHORIZED)The Authorization header was missing or invalid.
403 (FORBIDDEN)You do not have permission to perform the requested action.
404 (NOT FOUND)The resource at the location specified doesn't exist.
405 (METHOD NOT ALLOWED)The HTTP method used is not valid for the location specified.
409 (CONFLICT)The requested resource update could not be completed due to the current state of the target resource.
410 (GONE)The resource being accessed is no longer available.
413 (PAYLOAD TOO LARGE)The request entity was too large.
415 (UNSUPPORTED MEDIA)The requested content type is not supported (CDN only).
429 (TOO MANY REQUESTS)You are being rate limited, see Rate Limits.
502 (GATEWAY UNAVAILABLE)There was no gateway available to process your request. Wait a bit and retry.
503 (SERVICE UNAVAILABLE)The service required to process your request is unavailable. Wait a bit and retry.
5xx (SERVER ERROR) 1The server had an error processing your request.

1 502, 504, 507, 522, 523, 524 should be retried with a backoff. The number of failed requests can be specified with the X-Failed-Requests header. Whether you retry other 5xx errors is up to you.

JSON

Along with the HTTP error code, the API can also return more detailed error codes through a code key in the JSON error response. The response will also contain a message key containing a more friendly error string. Some of these errors may include additional details in the form of Error Messages provided by an errors object.

JSON Error Codes
Example JSON Error Response
{
"message": "Invalid authentication token",
"code": 50014
}