How to YouTube 🏄 in Contentful
Tags: contentful, tutorial, react, programming
Published: 2022-06-13 18:20:21

Welcome back everyone😃 Here we are with a new Contentful post. Today we are going to build a new integration using the App Framework 😁.

## Intro

Why do we build integrations? My answer, and probably the most common one is ... _**simplify the job to our final users/customers**_. The idea to help people in their daily job is satisfying to me.
What we are going to build is very simple and maybe an overkill but I want to demonstrate that even a small application can be useful to speed up your workflow (plus it has been fun to build it 🤓).
I'll skip some configuration steps so if you want to know how to setup a local environment you can follow this [guide](https://www.contentful.com/developers/docs/extensibility/app-framework/) or take a look at my [previous series](https://dev.to/wiommi/how-i-built-a-contentful-app-combined-with-commerce-js-i-5a77) on Contentful.

---

## The Concept

I work in the e-commerce business and the most exciting part is helping customers create and organise their content. In every project, there are always a few components that need to render a video and usually, it is a youtube video.
The most straightforward solution is to create a component with a simple text field in which the user needs to add the code or the embedded URL of the video. Here I see a few problems:

- Not every final user is technical, so you need to teach how to find the code/url. Maybe you need to write a guide with a few steps too.

- When you save this code, after a few days/weeks (maybe hours 😢) how can you understand easily which video it is?

- How many times a bad "copy&paste" leads to a bug opened in your board saying "the component is not working, I can't see the video" 🙄.

So the idea here is to build something that:

- It allows your customer not to leave the CMS interface.
- Provide a simple UI where you can search and preview your video.
- Once the video is saved, you give an overview of your selection.

Enough talking...Let's get it done 🥳 🚀

---

## YouTube Data API

We are going to use the YouTube Data API, so first, we need a valid API key that will be configured inside our Contentful App on the ConfigScreen location.

At the following [link](https://developers.google.com/youtube/v3/getting-started#before-you-start) you can find a guide on how to create your key. 
For our use case the key is enough. We don't need user authentication since we are retrieving only public data.

The last thing to keep in mind is the [Quota usage](https://developers.google.com/youtube/v3/getting-started#quota). We have access to a default 10,000 units per day which should be enough for us, but remember that every single search costs 100 units while every call to retrieve information about the selected video costs 1 + 1 units (one for the video endpoint and the other for the channel endpoint).

---

## Contentful: App Definition

The app definition is very simple. The idea is to extend the `short text` field type with our integration.


![Contentful: App Definition](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2zr4mjy16zbq6dkhtgt.jpg)

---

## Contentful: Config Screen

Now we can start with our first custom location. Inside the Config Screen, we can save our API key that would be accessible across our app as an installation parameter.


![Contentful: Config Screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7vejkeygdayrmn19teg.jpg)

---

## Contentful: Dialog

The Dialog location will be used as a container for our search.
The Modal will be triggered inside your Field location showing to our user a basic input text and a search button.

![Contentful: Dialog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/go8j92uou1qrora8j7bi.jpg)

As soon as you search for a valid term a grid of results is shown. If more than 50 results are available for your search term a load more cta will be available at the bottom of the grid (every 'load more' click counts as a search so 100 units in our quota usage)


![Contentful: Dialog Results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8w6l2mlusokds26lx6q.jpg)


![Contentful: Dialog Load More](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u931zzjrkzslg14w7uw7.jpg)


If you hover with your mouse on a single result, two ctas will appear. The 'preview' cta allows you to see the video before selecting it. The 'select' cta lets you save the video in your field.


![Contentful: Dialog Mouse over](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hz3gosl2hm60mwqm2o5n.jpg)

---

## Contentful: Field

Now our final location...
The Field lets you select/change/delete a video showing a preview card with some information about your selection.


![Contentful: Field - Empty](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0wz3j4w15kc9y9b41ll.jpg)


![Contentful: Field - Selected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52ocwe4oca2gz5yvl88h.jpg) 

---

## Conclusion

That's all folks! I hope you enjoyed the content.
Here you can find the repo with the project (https://github.com/williamiommi/contentful--youtube)

Thanks for reading!

See ya 🤙