How to Call and Connect with YouTube APIs from Salesforce

salesforce data migration services

How to Call and Connect with YouTube APIs from Salesforce

Posted by Thamaraikannan

YouTube and Salesforce are among the most powerful platforms available out there today. In this blog I am going to take you through a step-by-step process for accessing API’s with the help of Salesforce. I’ll be using an example that is familiar to everyone – YouTube. Using YouTube APIs, I’ll show you how to access video details, i.e. the number of views, likes, dislikes and comments for your favorite cat video!

Step 1: Create a Salesforce Custom Object
In the Salesforce Org create a Custom Object called ‘YouTube Video’. For those who have just started finding your way around Salesforce, follows these steps for creating a Custom Object.

  1. Click Setup → Quick Find / Search → Create → Objects → New Custom Object
  2. Name Your Custom Object as YouTube Video (Plural form will be YouTube Videos) and your record name will be YouTube Video Name.
  3. Click Saveyoutube custom object

Step 2: Add Custom Fields & Relationships
The next step is to add Custom Fields & Relationships to your newly created YouTube Video Custom Object. Create Number of View Counts, Number of Likes, Number of Dislikes, Number of Comments, Video Id as your Custom Fields.

field relationship salesforce

Number of View Counts : This Field will give how many Views the YouTube Videos have
Number of Likes              : This Field will give how many Likes the YouTube Videos have
Number of Dislikes          : This Field will give how many Dislikes the YouTube Videos have.
Number of Comments    : This Field will give how many Comments the YouTube Videos have
Video Id                            : This Field will show the unique YouTube Video Id for every video on YouTube

Hint: How do you fetch the YouTube Video Id?

In the below example, the highlighted section in the video URL refers to the Id for this particular video. Similarly, the Id will be displayed after equal sign in the URL for each video.

Youtube salesforce

Step 3: Create a Salesforce Custom App
Next, you need to create an App. In case you were wondering, here’s the path for that.
Setup → Quick Find / Search → Create → App → Custom App

youtube details

Step 4: Create a Custom Tab
Next step is to create a Custom Tab for that.

  1. Click Setup → Quick Find / Search → Create → Tabs → Custom Tabs → New
  2. In the Object Picklist, select the object that you have created and the tab style.
  3. Save the tab.

tab style

Step 5: Edit the YouTube Details Page

  1. From the App Menu on the extreme right corner, select your app and customize your App from the Navigation bar by clicking (+).
  2. Add YouTube Videos Tab to it.
  3. Edit the details page as given below in the image by using Edit Layout option.

youtube sf

Note: How do you send Http request and get it back from Google API?

http request

Step 6: Get the API Key

  1. Click the URL to start creating a new project. For YouTube API you need an End point and API Key or OAuth. In the current example, I will be using API Key.
  2. Click Google’s Developer Console on the extreme left of the navigation bar.
  3. Click Credentials → create Credentials → API key → Server key → Create. This will generate the API key for this call, which gives access to the YouTube API. Since I am accessing the API call from Salesforce, this key is just a token to get the required details from YouTube.

Google dev

Step 7: Enable YouTube Data API
For the second End Point, Select Google Developer Console → Overview → Google API → YouTube APIs → YouTube Data API and Enable it.

Google dev2

Step 8: Send Request and Get Response as Endpoint

  1. Click on Try this API in APIs Explorer → videos.list → part = statistics → id = YouTube Id → Authorize and Executeapi explore
  2. The result will give you the Request and API key in the format that needs to be used in the Custom Setting.api explorer 2

Step 9: Create Custom Settings

  1. Now you need to create Custom Setting for accessing YouTube API from Salesforce.
    Setup → Quick Find / Search → Develop → Custom Setting → New
  2. Name the custom setting as YouTube API Setting.
  3. Create custom field as Value with DataType as Text api setting
  4. Click Manage in Custom Setting Definition Detail page and create two fields called YouTube Endpoints and YouTube API Key.
  5. In the Endpoint field, Paste the Request that was generated in the previous step from the YouTube details and also mention the statistics and Id.
  6. In the API field, Paste the API key that you got from Credentials (Custom settings are used for storing the API Key and encoded key).youtube api setting 2

Step 10: Create Remote Site Settings

  1. Call external API from Salesforce with the help of Remote Site Setting through Setup → Quick Find / Search → Security Controls → Remote Site Settings → New
  2. Remote Site Name: You__Tube__API__Setting
  3. Remote Site URL: and make it active.remote site details

Step 11: Create New Record

  1. On the navigation bar, click YouTube Videos and start creating a new record.
  2. Fill the Name and YouTube Id field, leave the other fields empty and save it.

youtube sf

Once saved, you can see that the default buttons available are Edit, Delete and Clone. In our case, the clone button is not relevant, instead of which you need a Get Details button.

youtube sf 2

Step 12: How to Create Get Details Button?

  1. Setup → Quick Find / Search → Create → object → (Custom object) YouTube Video → Buttons, Links and Actions → New Button or Link → Name the Button as Get Details
  2. To display Get Details button in the YouTube Video details page, go to YouTube Video Details page → Click on Quick Access menu on the extreme right.
  3. Click Edit Layout → Buttons → Drag and drop the Get Details button to the YouTube Details page.

Step 13: Create Apex Class
Create Apex Class in the name of  YouTube Video Details by Setup → Quick Find / Search  → Develop →  Apex Classes → New

The Code for the class is given in this link

Step 14: Add JavaScript to Get Details Button
Clicking the Get Details button on the YouTube Details page should display the details of a particular video. Here are the steps for doing this.

1.Add code to the button using JavaScript. Here’s the code for that

2.For adding a code to the Get Details button, follow this path
Setup → Quick Find / Search → Create → objects → YouTubeVideo ( custom object that we created) → Buttons, Links, and Actions → Get Details. Then, add the code as shown in the  below screenshot.

get details

3.Click the Get Details button in the YouTube Details Page.

salesforce video

Step 15: Final Output
You final output will display the details for your chosen video – the number of View Counts, Likes, Dislikes and Comments as given below.

salesforce video 2

This is just one of the many instances where you can use an external APIs with Salesforce. If you have come across similar examples, do share them in the comments section below. It may become an inspiration for another fun Salesforce project!