This tutorial is perfect for those that need to send discord channel invites via the discord api to users via php. At this time I’m not going to walk you through creating the bot token in this tutorial, but if you would like me to please reach out to me on discord.

Why I Created this tutorial?

I’m making this tutorial to help someone out that needs to add the discord invite functionality to their website, also to save the dev a headache when trying to figure out why they may be getting a 401 unauthorized error when using the discord api.

The 401 unauthorized access error seems to be really common occurrence.

This is the error I was getting when i started trying to code this for a client of mine, and i could not find an answer to what i was looking for online. When I started trying to connect to the discord api to send new customers a Discord channel invite I kept getting back an error an I could not figure out why. Here is an example of the error I kept getting back from this endpoint{}/invites.

    "code": 0,
    "message": "401: Unauthorized"

Why did the 401 unauthorized access error get returned to me from the discord api?

My Problem was I was not using the correct token type. So when I would send a request with guzzlehttp to the discord api, I would get back a 401 unauthorized response. The token type I was using at this time was a Bearer token that was generated via oAuth2, Unfortunately users don’t have authorization to mess with a bunch of discord api endpoints. Most API endpoints seem to be accessible to only bot tokens. If anyone would like to see how I figured out how to solve this issue let me know. I record everything I do on a daily basis.

Finally what you probably came here for, the code used to make the request to create a discord invite with the api.

Actually connecting to the api and getting back Json results that contain existing invite codes that can be sent to users via email or via another method.

require __DIR__ . '/vendor/autoload.php';

$client = new \GuzzleHttp\Client();

$res = $client->request('GET', '', [
    'headers' => 
            'Authorization' => 'Bot' . ' ' . 'Bot-token',
            'Content-type’ => ‘application/json'

$res = json_decode( $res->getBody()->getContents() );

// Testing out the results var_dump($res);

// if i had more than one result here i might need to do a loop here an figure out which invite code i want to use to send the user the correct invite code.
// var_dump($res[0]->code);

foreach ($res as $key => $value) {

    //this would output all of the codes for that channel.
    // $res[$key]->code;

    // you may want to output a code based on the username that creates the invite code.

    if($res[$key]->inviter->id == 593907095187881995){
        echo $res[$key]->code;
How to go about creating a new invite code for a discord channel via php.

The max_age can have values between (0 – 86,000). When max_age is set to ‘0’ it will create an invite link that does not expire. By the way from my experience you unfortunately cannot have two different invite codes for one channel that is created by a bot that sets the same max_age value. The only way to create a new invite code that uses the same max_age value is once it has expired.

$inviteCode = $client->request('POST', '', [
    'headers' => [
            'Authorization' => 'Bot' . ' ' . 'Bot-token',
            'Content-type’ => ‘application/json'
    'json'  => [
            'max_age' => 0

$inviteCode = json_decode( $inviteCode->getBody()->getContents() ); 
//the invite code 
$newinviteCode = $inviteCode->code;

After creating the invite code and you have received a response, you can use the results in the invite code variable to send users an invite link via email or via another method. Of course you will have to create an invite link with that code.

Sending the invite code to the user with php.

Here is some sample code of what you can do to send the user an invite code, and generate an invite link that is emailed out to a specific email.

$invite_link = '' . $inviteCode->code;
$to_email = "";
$message = 'thanks for signing up to our site. If you would like to join us on discord here is your invite' . $invite_link;

mail($to_email, $message);

If you have any questions at all for me please ask on discord.

Website Guides