Using Node.js

How to Scrape YouTube Video Details with Node.js

Get complete information about a YouTube video or short using Node.js. This comprehensive guide will walk you through the entire process, from setup to implementation.

Overview

What You'll Learn

  • Setting up your Node.js environment
  • Installing the required HTTP client
  • Authenticating with SociaVault API
  • Making requests to YouTube
  • Handling responses and errors

What You'll Get

  • Access to video data
  • JSON formatted responses
  • Real-time data access
  • Scalable solution
  • Error handling patterns

Prerequisites

1. API Key

First, you'll need a SociaVault API key to authenticate your requests.

2. Development Environment

Make sure you have the following installed:

  • Node.js installed
  • A code editor (VS Code, Sublime, etc.)
  • Command line interface access

Implementation

Step 1: Install HTTP Client

We'll use axios to make HTTP requests.

bash
npm install axios

Step 2: API Implementation

Now let's make a request to the YouTube API using Node.js. Replace YOUR_API_KEY with your actual API key.

nodejs
import axios from 'axios';

const API_KEY = 'YOUR_API_KEY';

async function scrape() {
  try {
    const response = await axios.get(`https://api.sociavault.com/youtube/video?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbjVIDXPP7Uk`, {
      headers: {
        'x-api-key': API_KEY,
        'Content-Type': 'application/json'
      }
    });

    console.log('Response:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error:', error.response?.data || error.message);
  }
}

// Usage
scrape();

Testing Your Code

API Parameters

ParameterTypeRequiredDescription
urlstringYesExample: https://www.youtube.com/watch?v=bjVIDXPP7Uk

Expected Response

You should receive a structured JSON response containing the video data.

json
{
  "id": "Y2Ah_DFr8cw",
  "thumbnail": "https://img.youtube.com/vi/G6VTenw0S7o/maxresdefault.jpg",
  "type": "video",
  "title": "Inside the NBA: Chuck Trolls Jussie Smollett \"Do not commit crimes with checks, use cash!\"",
  "description": "Description here",
  "commentCountText": "347",
  "commentCountInt": 347,
  "likeCountText": "3.8K",
  "likeCountInt": 3,
  "viewCountText": "358,277",
  "viewCountInt": 358277,
  "publishDateText": "Feb 22, 2019",
  "publishDate": "2019-02-22T00:00:00.000Z",
  "channel": {
    "id": "UCWH3hing1Qb4LnkRfQdxsxQ",
    "url": "https://www.youtube.com/@afroballer8906",
    "handle": "afroballer8906",
    "title": "Afroballer"
  },
  "durationMs": 1670000,
  "durationFormatted": "00:27:50",
  "watchNextVideos": [
    {
      "id": "fRfkvQwf9Po",
      "title": "Inside the NBA funniest moments of all time",
      "thumbnail": "https://i.ytimg.com/vi/fRfkvQwf9Po/hqdefault.jpg?sqp=-oaymwFACKgBEF5IWvKriqkDMwgBFQAAiEIYAdgBAeIBCggYEAIYBjgBQAHwAQH4Af4JgALQBYoCDAgAEAEYfyBHKCgwDw==&rs=AOn4CLAnLkcTjwH2egq1U823zjwxx_63DA",
      "channel": {
        "title": "Beyond NBA",
        "url": "https://www.youtube.com/@beyond-NBA",
        "handle": "beyond-NBA",
        "id": "UCPGhgqNOGTj1o1qilS6K8mA"
      },
      "publishDateText": "5 years ago",
      "publishDate": "2020-02-27T18:26:04.534Z",
      "viewCountText": "7,913,223 views",
      "viewCountInt": 7913223,
      "lengthText": "19:13",
      "videoUrl": "https://www.youtube.com/watch?v=fRfkvQwf9Po"
    }
  ],
  "keywords": [
    "charles barkley"
  ],
  "transcript": [
    {
      "text": "it was a a lead of 13 for the Rockets up",
      "startMs": "0",
      "endMs": "5759",
      "startTimeText": "0:00"
    }
  ],
  "transcript_only_text": "it was a a lead of 13 for....play okay the Lakers will not make the playoffs the Kings will is the latest position go ahead Kenny I cannot leave you chuck here we caster do payable to answer a rattle check check never break America America let me just tell you something is that do not commit crimes with check come on man you cannot if you're gonna break the law I do not write a check because you write a check that what I never used ATM now you can only I heard you could only get $200 out of the holes oh stop America America you go hot write checks when you commit illegal activity I cannot believe that we completed we touch them all right there circle the bag he just said he punch every pillow I wasted all that damn town of money you know what you should have been was bad that's what I'm really amazing neighborhood Charles"
}

Best Practices

Error Handling

Implement comprehensive error handling and retry logic for failed requests. Log errors properly for debugging.

Caching

Cache responses when possible to reduce API calls and improve performance. Consider data freshness requirements.

Security

Never expose your API key in client-side code. Use environment variables and secure key management practices.

Troubleshooting

401

Unauthorized

Check your API key is correct and properly formatted in the x-api-key header.

402

Payment Required

You ran out of credits and need to buy more.

404

Not Found

The resource (user, video, etc.) might not exist or be private.

429

Too Many Requests

You have exceeded your rate limit. Slow down your requests.

Frequently Asked Questions

How much does it cost to scrape YouTube?

SociaVault offers 50 free API calls to get started. After that, pricing starts at $10 for 5k requests with volume discounts available.

Is it legal to scrape YouTube data?

Scraping publicly available data is generally considered legal. We only collect public data that is accessible without logging in.

How fast can I scrape YouTube?

Our API handles the rate limiting for you. You can make requests as fast as your plan allows.

What data format does the API return?

All API responses are returned in JSON format, making it easy to integrate with any programming language or application.

Related Tutorials

Ready to Start Scraping?

Get started with 50 free API calls. No credit card required. Stop worrying about proxies and captchas.