1

I have made a program where users provide a YouTube channel URL and in return I will show the user its channel ID.

Like if someone puts URL like: https://youtube.com/@AndreoBee then I will return its channel ID.

const axios = require('axios');

async function getChannelId(url) {
  const channelUsername = url.split('/').pop();
  const response = await axios.get(`https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=${channelUsername}&key=myapi`);

  if (response.data.items.length === 0) {
    throw new Error(`Channel not found: ${url}`);
  }

  console.log(response.data.items[0].id);
}

getChannelId('https://www.youtube.com/@AndreoBee');

Output:

/home/runner/ytcheck/index.js:7 if (response.data.items.length === 0) { ^

TypeError: Cannot read properties of undefined (reading 'length') at getChannelId (/home/runner/ytcheck/index.js:7:27)

isherwood
  • 58,414
  • 16
  • 114
  • 157
Ansh Tyagi
  • 116
  • 1
  • 15

3 Answers3

1

One alternative - using YouTube Data API - could be:

  • Use the search:list endpoint for search channels that matches with the criteria - in this case, the name/handle of the channel.

Example:

Search channels named "AndreoBee" - you're getting this value in the channelUsername variable in your code:

URL:

https://youtube.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=10&q=AndreoBee&type=channel&key=[YOUR_API_KEY]

Response:

{
  "kind": "youtube#searchListResponse",
  "etag": "K2G-sXJIFnrJFyMaoDvw4XMBym4",
  "nextPageToken": "CAoQAA",
  "regionCode": "CO",
  "pageInfo": {
    "totalResults": 6740,
    "resultsPerPage": 10
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "oeyNublAqxB8TOEQbtwA4k0Oqgs",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCjdjyjdcdmldCK5XuuWbn1A"
      },
      "snippet": {
        "publishedAt": "2020-01-21T15:12:27Z",
        "channelId": "UCjdjyjdcdmldCK5XuuWbn1A",
        "title": "AndreoBee",
        "description": "I am a Youtuber.",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJW_gCXDGAviuBWh0GwUO4V5Ia4JNYzC0Mp9bYhigA=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJW_gCXDGAviuBWh0GwUO4V5Ia4JNYzC0Mp9bYhigA=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJW_gCXDGAviuBWh0GwUO4V5Ia4JNYzC0Mp9bYhigA=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2020-01-21T15:12:27Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "iG8-NwZab0LLB245AY2MkJ6pexI",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UC2Xpzteb9hbamaeEHZRxPCw"
      },
      "snippet": {
        "publishedAt": "2020-11-27T07:07:23Z",
        "channelId": "UC2Xpzteb9hbamaeEHZRxPCw",
        "title": "AndreoBee",
        "description": "This is my backup channel guys ;)",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJVgMinAIKpEHBPjM2Os8fHFKcsxtTyTIflqTwVR=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJVgMinAIKpEHBPjM2Os8fHFKcsxtTyTIflqTwVR=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJVgMinAIKpEHBPjM2Os8fHFKcsxtTyTIflqTwVR=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2020-11-27T07:07:23Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "3X2zApHnPAW2p2awm4tIETn9ybI",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCp6iawa7GPTszoJxOiG5d_Q"
      },
      "snippet": {
        "publishedAt": "2021-03-15T16:19:39Z",
        "channelId": "UCp6iawa7GPTszoJxOiG5d_Q",
        "title": "AndreoBee",
        "description": "Welcome my ARS GAMING YouTube channel I am uploading full movies or shorts and how to download movies on my YouTube ...",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/Z2CC1zWbsD3k-z9cX0RlPBstKELZw0_sUgFPggIt-DhVkJWL8iJG8pCLizxr3StB8lbZX9PT7U8=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/Z2CC1zWbsD3k-z9cX0RlPBstKELZw0_sUgFPggIt-DhVkJWL8iJG8pCLizxr3StB8lbZX9PT7U8=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/Z2CC1zWbsD3k-z9cX0RlPBstKELZw0_sUgFPggIt-DhVkJWL8iJG8pCLizxr3StB8lbZX9PT7U8=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2021-03-15T16:19:39Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "VHP4HsDcIiETPleAWN0cSC7fvbE",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCWLY3sMMYHH3-9xQ2lhFj8g"
      },
      "snippet": {
        "publishedAt": "2022-06-27T01:08:10Z",
        "channelId": "UCWLY3sMMYHH3-9xQ2lhFj8g",
        "title": "AndreoBee",
        "description": "Hii guus i am Andreo I hope you like my videos and please support me guys thank you have a good day.",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/bHaKoA8QZnZSJXfmK7udtrzxmpCOOhpTHIoP_RQUD2KLv9qNXtLLKY_bewbaQGdfZGIPCcO9Ag=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/bHaKoA8QZnZSJXfmK7udtrzxmpCOOhpTHIoP_RQUD2KLv9qNXtLLKY_bewbaQGdfZGIPCcO9Ag=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/bHaKoA8QZnZSJXfmK7udtrzxmpCOOhpTHIoP_RQUD2KLv9qNXtLLKY_bewbaQGdfZGIPCcO9Ag=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2022-06-27T01:08:10Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "01NvBqsdnNimHwnwHnGkeVOhJ-8",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UC3yVv5tuR3tBcVf97j33mQQ"
      },
      "snippet": {
        "publishedAt": "2020-12-12T14:31:22Z",
        "channelId": "UC3yVv5tuR3tBcVf97j33mQQ",
        "title": "Andreobee",
        "description": "Describe the game All best game for play on Android in 2GB ram and 3GB phone.",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJXval25GhQFpbqoFgQXvWBol5aqf4ZCOluMMOe-=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJXval25GhQFpbqoFgQXvWBol5aqf4ZCOluMMOe-=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJXval25GhQFpbqoFgQXvWBol5aqf4ZCOluMMOe-=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "Andreobee",
        "liveBroadcastContent": "none",
        "publishTime": "2020-12-12T14:31:22Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "B5TOx4aFyuua9eQVa-QBbKDjIng",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCBR0QlbTtbT52oFmmDZIamw"
      },
      "snippet": {
        "publishedAt": "2022-02-01T14:44:18Z",
        "channelId": "UCBR0QlbTtbT52oFmmDZIamw",
        "title": "AndreoBee",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/TfGkwtvsAvJdWnMOdRLRn2HKRvnVj872dxLZdZ_ghFwpF4SrY2BqBwJ3syc1V6HLL3LyJiSABQ=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/TfGkwtvsAvJdWnMOdRLRn2HKRvnVj872dxLZdZ_ghFwpF4SrY2BqBwJ3syc1V6HLL3LyJiSABQ=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/TfGkwtvsAvJdWnMOdRLRn2HKRvnVj872dxLZdZ_ghFwpF4SrY2BqBwJ3syc1V6HLL3LyJiSABQ=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2022-02-01T14:44:18Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "t04J8v3aRhQhmFsyv54ddTXGDMQ",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCBvdbv05Cb4Ulf_PlunMJtA"
      },
      "snippet": {
        "publishedAt": "2021-08-12T06:59:40Z",
        "channelId": "UCBvdbv05Cb4Ulf_PlunMJtA",
        "title": "AndreoBee",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/S3J1R-s_P481lUpBULskSiQ0Pv-Z_iLnE_YrZ10OL1cydCFx6a1aOXvRJskMxY-gVpa8cqwuzQ=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/S3J1R-s_P481lUpBULskSiQ0Pv-Z_iLnE_YrZ10OL1cydCFx6a1aOXvRJskMxY-gVpa8cqwuzQ=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/S3J1R-s_P481lUpBULskSiQ0Pv-Z_iLnE_YrZ10OL1cydCFx6a1aOXvRJskMxY-gVpa8cqwuzQ=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2021-08-12T06:59:40Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "dmhfSWj6r1CzXr4yopXkorSsVL0",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCvXV0HSenVdIpdAuogNPuOQ"
      },
      "snippet": {
        "publishedAt": "2022-11-02T12:01:42Z",
        "channelId": "UCvXV0HSenVdIpdAuogNPuOQ",
        "title": "AndreoBee",
        "description": "PLZ  SUB.....",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/NfH1RxDcGEbLe8cOoi7VwFz-dfUcAToV-84rag4x1CIQM8GSnZirguR5SI30M3bZnMP80tA=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/NfH1RxDcGEbLe8cOoi7VwFz-dfUcAToV-84rag4x1CIQM8GSnZirguR5SI30M3bZnMP80tA=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/NfH1RxDcGEbLe8cOoi7VwFz-dfUcAToV-84rag4x1CIQM8GSnZirguR5SI30M3bZnMP80tA=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBee",
        "liveBroadcastContent": "none",
        "publishTime": "2022-11-02T12:01:42Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "c5Y3zUOZKiyJ-pI7mrLdi4WPegc",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCmrR9PVxSVRLdp2HK_xP2yA"
      },
      "snippet": {
        "publishedAt": "2021-09-13T06:16:47Z",
        "channelId": "UCmrR9PVxSVRLdp2HK_xP2yA",
        "title": "ANDREOBEE",
        "description": "AndreoBee fan Club For business inquiries ANDREOBEEofficial.",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/CtsMEUKLJC5apm5bX7vEr_3g7nIJn-GfjLodWXLVLF-jFGfyXhFBTiLUvOU8l51e3kVPph5MUQ=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/CtsMEUKLJC5apm5bX7vEr_3g7nIJn-GfjLodWXLVLF-jFGfyXhFBTiLUvOU8l51e3kVPph5MUQ=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/CtsMEUKLJC5apm5bX7vEr_3g7nIJn-GfjLodWXLVLF-jFGfyXhFBTiLUvOU8l51e3kVPph5MUQ=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "ANDREOBEE",
        "liveBroadcastContent": "none",
        "publishTime": "2021-09-13T06:16:47Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "-lb0jmjUJcQsLJDGHH7KVs14-nU",
      "id": {
        "kind": "youtube#channel",
        "channelId": "UCvqm9nqHYYehmq7FcorjS4Q"
      },
      "snippet": {
        "publishedAt": "2021-01-13T05:54:00Z",
        "channelId": "UCvqm9nqHYYehmq7FcorjS4Q",
        "title": "AndreoBEE",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJUIkodf8aTOx2ZcNoyycMHQqgqNvehuXN_iz9Ii=s88-c-k-c0xffffffff-no-rj-mo"
          },
          "medium": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJUIkodf8aTOx2ZcNoyycMHQqgqNvehuXN_iz9Ii=s240-c-k-c0xffffffff-no-rj-mo"
          },
          "high": {
            "url": "https://yt3.ggpht.com/ytc/AL5GRJUIkodf8aTOx2ZcNoyycMHQqgqNvehuXN_iz9Ii=s800-c-k-c0xffffffff-no-rj-mo"
          }
        },
        "channelTitle": "AndreoBEE",
        "liveBroadcastContent": "none",
        "publishTime": "2021-01-13T05:54:00Z"
      }
    }
  ]
}

N.B: for this case, I will accept that the first result is the channel I'm looking for, however, notice here that you might get more than one channel that matches the channel name.

For detect which one of the results of the search matches with the channel you're looking for, you could then, make a request to the channel:list endpoint - by passing the channel_id of the search result - and see their customUrl value and/or check the channel that has more videos/subscribers/etc.

Check my answer for more info.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
1

You can try the following node.js script

var https = require('https');

var url = "https://www.youtube.com/@adnantoky"

https.get(url, function (res) {
    var data = '';
    res.on('data', function (chunk) {
        data += chunk;
    });
    res.on('end', function () {
        let arr = data.split('channel_id=');
        console.log("Channel ID:", arr[1].slice(0, 24));
    });
}).on('error', function (e) {
    console.log(e.message);
});
Adnan Toky
  • 1,756
  • 2
  • 11
  • 19
-1

You can get a channel id using /search Youtube API. Instead using this :

https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=${channelUsername}&key=myapi

Try with this endpoint :

https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&q=${channelUsername}&type=channel&key=myapi
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Jordy
  • 1,802
  • 2
  • 6
  • 25
  • It is not giving the correct Id. Correct ID = UCjdjyjdcdmldCK5XuuWbn1A it is giving = UCnmkfAgbsF2dPt4DL2ZqUrQ – Ansh Tyagi Feb 07 '23 at 12:08
  • Have you tried with other index? `response.data.items[1].id` – Jordy Feb 07 '23 at 12:15
  • The problem is that it is not showing the channel I need it is showing all other channels – Ansh Tyagi Feb 07 '23 at 13:31
  • 1
    How about this [https://yt.lemnoslife.com/channels?handle=@AndreoBee](https://yt.lemnoslife.com/channels?handle=@AndreoBee) – Jordy Feb 07 '23 at 13:40
  • ok this url worked. Tell me one thing does this url also contains limits like google apis – Ansh Tyagi Feb 07 '23 at 13:53
  • 1
    According to the [https://yt.lemnoslife.com/](https://yt.lemnoslife.com/), there are no limitations at this moment, but future that can happen – Jordy Feb 07 '23 at 14:11