<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sreeram Venkitesh</title>
    <description>The latest articles on Forem by Sreeram Venkitesh (@fillerink).</description>
    <link>https://forem.com/fillerink</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F420912%2F2b08c614-c126-4955-9f8c-0de4baa73906.jpg</url>
      <title>Forem: Sreeram Venkitesh</title>
      <link>https://forem.com/fillerink</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fillerink"/>
    <language>en</language>
    <item>
      <title>Getting Started with Twitter APIs using Deta</title>
      <dc:creator>Sreeram Venkitesh</dc:creator>
      <pubDate>Wed, 29 Jul 2020 11:47:40 +0000</pubDate>
      <link>https://forem.com/deta/how-i-used-deta-and-the-twitter-api-to-update-my-profile-name-with-my-follower-count-tom-scott-style-l1j</link>
      <guid>https://forem.com/deta/how-i-used-deta-and-the-twitter-api-to-update-my-profile-name-with-my-follower-count-tom-scott-style-l1j</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=hTHEq4MjHHc"&gt;The video tutorial to this post&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You must have seen Tom Scott's YouTube &lt;a href="https://www.youtube.com/watch?v=BxV14h0kFs0"&gt;video&lt;/a&gt; where he uses the YouTube API to automatically update the title of the video. I had written a tutorial for the same in a previous article (Check it out &lt;a href="https://dev.to/deta/how-to-use-the-youtube-api-to-create-a-self-updating-video-title-based-on-views-59o8"&gt;here&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;After doing the YouTube project, I was thinking if I could do the same with my Twitter &lt;a href="https://twitter.com/fillerInk"&gt;account&lt;/a&gt;. I tried reading through the Twitter API docs and it turned out doing this was actually pretty easy! I was able to completely write and deploy the code in an hour.&lt;/p&gt;

&lt;p&gt;In this post, I'll walk you through how I did it and how you can easily get started with the Twitter API to automate processes like updating your profile, posting tweets etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an app with Deta
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create an empty folder for your project&lt;/li&gt;
&lt;li&gt;From the folder, run &lt;code&gt;deta new --node twitter-update&lt;/code&gt; to create a new Deta Micro. &lt;/li&gt;
&lt;li&gt;Once the app is created, you will get some details including the endpoint web address. We will use this to create an app from the Twitter Developer Console.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting up the Twitter App
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ezdFhfme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8e1o5zswn7an67869mc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ezdFhfme--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8e1o5zswn7an67869mc4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in to your Twitter Developer account and navigate to your dashboard.&lt;/li&gt;
&lt;li&gt;Create a new app and give it a suitable name. Use the endpoint that you got when creating the Micro as the website url for your app&lt;/li&gt;
&lt;li&gt;Go to the keys and tokens tab to find your API Key and API Secret Key.&lt;/li&gt;
&lt;li&gt;Under the API keys, click the option to generate your access token and secret. Copy and store these keys somewhere safe as you will only get to see it once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--38lX23Yk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/abost565ee29ej2w7kdh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--38lX23Yk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/abost565ee29ej2w7kdh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Before writing the code
&lt;/h2&gt;

&lt;p&gt;Now that we have the necessary keys, lets get to writing the code that actually does the work. But we need to import the API keys and access tokens inside our code inorder to use it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;.env&lt;/code&gt; file inside your project and copy the credentials you got from the Twitter App Dashboard into it, as shown
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;consumer_key=blah
consumer_secret=blahblah
access_token_key=blahblahblah
access_token_secret=blahblahblahblah
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now that you have a .env file, you can access these from inside your code (You might have to install the &lt;code&gt;dotenv&lt;/code&gt; package and set &lt;code&gt;require('dotenv').config()&lt;/code&gt; inside your js file. I assume you've done this already.)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Writing the code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We are going to use the &lt;a href="https://www.npmjs.com/package/twitter"&gt;Twitter for Node.js package&lt;/a&gt; for making requests to the API.&lt;/li&gt;
&lt;li&gt;Install the package with &lt;code&gt;npm install twitter&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Import the twitter package and create a client object
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;twitter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;twitter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;twitter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;consumer_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;consumer_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;consumer_secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;consumer_secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;access_token_key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;access_token_key&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;access_token_secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;access_token_secret&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you can call the get and post functions of the client object to use the Twitter APIs however you want.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can refer to the &lt;a href="https://developer.twitter.com/en/docs"&gt;docs&lt;/a&gt; to choose an api and study about its parameters and such.&lt;/p&gt;

&lt;p&gt;In our example, we need to read the profile details from our account - the name, the number of followers and so on and use it to update a part in the profile, the name.&lt;/p&gt;

&lt;p&gt;Let's see how to read the profile first. We can use &lt;code&gt;account/verify_credentials&lt;/code&gt; to fetch the details from our Twitter account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;account/verify_credentials&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;followerCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;followers_count&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;followerCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we retrieve only the &lt;code&gt;followers_count&lt;/code&gt; from the response, but you can try printing the entire response object to see what all information we are fetching.&lt;/p&gt;

&lt;p&gt;Now we need to update the profile details. For this we can use &lt;code&gt;account/update_profile&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;account/update_profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Updated&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of code above will update the name parameter in your profile to whatever value you give it. You can even create a params object with all the stuff you want to change and pass it to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;account/update_profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Name has been updated&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that you have the two pieces of the puzzle, you can use the updation inside the function where you fetch the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;account/verify_credentials&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cnt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;followers_count&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sree has ${cnt} followers!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;account/update_profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;,(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Name has been updated&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;            
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you run npm start and start the server locally, your Twitter profile name will be updated. But we need this to happen in the background, in real time. This is where Deta Cron comes in.&lt;/p&gt;

&lt;p&gt;A cron is a piece of code that has been scheduled to run at a certain interval, and it keeps running again and again. With Deta you can easily deploy a cron with just a few lines of code and a few commands.&lt;/p&gt;

&lt;p&gt;For setting a cron with Deta, you can import App from Deta as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;deta&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And set the function to run as the cron inside &lt;code&gt;app.lib.cron&lt;/code&gt; like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cron&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The code here will be executed repeatedly      &lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you can bundle up all the above code we used to call the API into a function and call the function from inside here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now from your project directory, run &lt;code&gt;deta update -e .env&lt;/code&gt; to update your environment variables.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;deta deploy&lt;/code&gt; to deploy your code to production.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;deta cron set '1 minute'&lt;/code&gt; to set the code to run every minute.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Checkout &lt;a href="https://deta.sh"&gt;Deta&lt;/a&gt;, the cloud for the next billion ideas
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Get the code to this project at my &lt;a href="https://github.com/fillerInk/twitter-name-update"&gt;GitHub&lt;/a&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  See the name updation effect live on &lt;a href="https://twitter.com/fillerInk"&gt;my Twitter profile&lt;/a&gt;
&lt;/h3&gt;

</description>
      <category>javascript</category>
      <category>deta</category>
      <category>tutorial</category>
      <category>backend</category>
    </item>
    <item>
      <title>How to use the YouTube API to create a self-updating video title based on views</title>
      <dc:creator>Sreeram Venkitesh</dc:creator>
      <pubDate>Tue, 21 Jul 2020 10:31:56 +0000</pubDate>
      <link>https://forem.com/deta/how-to-use-the-youtube-api-to-create-a-self-updating-video-title-based-on-views-59o8</link>
      <guid>https://forem.com/deta/how-to-use-the-youtube-api-to-create-a-self-updating-video-title-based-on-views-59o8</guid>
      <description>&lt;h2&gt;
  
  
  &lt;a href="https://youtu.be/QwecvVvESVU"&gt;Link to my video tutorial&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You might have seen Tom Scott's &lt;a href="https://www.youtube.com/watch?v=BxV14h0kFs0"&gt;video&lt;/a&gt; where he uses the YouTube API to automatically update the title of his video in real time to say 'This video has n views'. Sometime after, another YouTuber, Mr.Beast did the &lt;a href="https://www.youtube.com/watch?v=YSoJPA8-oHc"&gt;same thing&lt;/a&gt; with the thumbnail of his video to show how much money he donated.&lt;/p&gt;

&lt;p&gt;One of my favourite dev channels, Fireship had posted a &lt;a href="https://www.youtube.com/watch?v=JjXBrJfp5TE&amp;amp;t=257s"&gt;video&lt;/a&gt; explaining how these people are doing this, explaining how APIs work and how they can be used to automate such tasks.&lt;/p&gt;

&lt;p&gt;Although Tom Scott never went into the intricacies of the code behind this process, Fireship explained how to acheive this to a fair degree of detail. &lt;/p&gt;

&lt;p&gt;Basically we need to use the YouTube Data API provided by Google to fetch the details of a video. We have two ways to do this&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use an API key to do read-only processes (Like retrieving the number of views)&lt;/li&gt;
&lt;li&gt;Use an OAuth 2.0 credential to sign in to your YouTube account and be able to retrieve and update the data. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We would need to use OAuth since we need to update the title of our video.&lt;/p&gt;

&lt;p&gt;Fireship's video, although really well made and interesting to watch, is not a tutorial. You can definitely get a head start but not a walkthrough. But I decided to give it a try nonetheless in the hope to being able to document the process and create a more concise tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1 - Using the YouTube Data API
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W2H2erQx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nshc69tjtjkw3c23l5ht.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W2H2erQx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nshc69tjtjkw3c23l5ht.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The API that we're gonna use is has an &lt;a href="https://developers.google.com/youtube/v3/docs/?apix=true"&gt;extensive documentation&lt;/a&gt; along with an in-browser API Explorer that you can use to test an API before actually using it in your project. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fsv2I_rx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jpdvc3wqz1i2oen5tbgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fsv2I_rx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jpdvc3wqz1i2oen5tbgy.png" alt="Alt Text"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This can be really handy to test the API while writing the code. Notice how there are two options for selecting the credentials. If you checked Google OAuth and select a scope within it (different scopes have different access levels), you'll be prompted to login to your Google account to authenticate the API request. This way the API can make sure that you are requesting to edit the details of a video posted from your account which you have the access to update.&lt;/p&gt;

&lt;p&gt;For the title we are going to need the &lt;code&gt;list&lt;/code&gt; and the &lt;code&gt;update&lt;/code&gt; functions of the API, both of which you can test from the explorer. &lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 - Creating a project with Deta
&lt;/h2&gt;

&lt;p&gt;Being able to call the API and change the video title is not enough. You should be able to do this periodically as the views get updated in real time. &lt;/p&gt;

&lt;p&gt;Using Deta makes setting up all of this really easy with their &lt;a href="https://docs.deta.sh/docs/micros/about"&gt;Micros&lt;/a&gt; and &lt;a href="https://docs.deta.sh/docs/micros/cron"&gt;Cron&lt;/a&gt;. You can write a piece of code, deploy it, set a cron and it will keep running periodically after a time period that you specify. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using Deta Micros and Crons to deploy your code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a new Micro&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inside your project directory, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deta new &lt;span class="nt"&gt;--node&lt;/span&gt; my-project-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new directory with the name of your project and it will have a &lt;code&gt;.deta&lt;/code&gt; folder inside, with all the information regarding your micro. (If you haven't used Deta before, you'll need to install the CLI and login from your machine. Check out a tutorial &lt;a href="https://docs.deta.sh/docs/micros/getting_started"&gt;here&lt;/a&gt;). Once the project is created, you can &lt;code&gt;npm init&lt;/code&gt; inside the directory to initialise a node project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install the node package for using Google APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From your project directory, once you've initialised the node project, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;googleapis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using the APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have the package installed, you can easily access all of Google's APIs for say, YouTube or Drive or whatnot.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;google&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;googleapis&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;youtube&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;google&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;youtube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;v3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here v3 represents DataAPI v3 of YouTube.&lt;/p&gt;

&lt;p&gt;You can now use the list function to fetch the video details and the update function to update the details of your video.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;youtube&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;videos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR_VIDEO_ID&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;part&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;snippet,statistics&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The API returned an error: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//Do something here with the data&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OK, so you might be confused with two things I didn't talk about yet. &lt;/p&gt;

&lt;p&gt;The auth object we pass to the function are our credentials. This will be the API Key if you only need to read the data or it'll be the access token and refresh token linked to your Google account that has the access to edit the title of the video. &lt;/p&gt;

&lt;p&gt;The other arguments for the function are the video ID, which is just the unique string present in the url of every video. The part argument specifies what all types of detail we need to fetch. &lt;code&gt;snippet&lt;/code&gt; stands for all the details of the video like the title, categoryId, description, tags etc. &lt;code&gt;statistics&lt;/code&gt; is information like the number of views, likes, dislikes and all. Since we need both the title and the view count, we can retrieve both the snippet and statistics of the video.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Updation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you get the details of a video, you can take the view count, create a string saying whatever you want with the view count and send it back to update it as the new title of the video.&lt;br&gt;
Here's how we can do it with code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;viewCount&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statistics&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTitle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`This video has got &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;viewCount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; views!`&lt;/span&gt;

&lt;span class="nx"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;snippet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newTitle&lt;/span&gt;

&lt;span class="nx"&gt;youtube&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;videos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    
    &lt;span class="na"&gt;part&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;snippet&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;//since we only need to update the title&lt;/span&gt;
    &lt;span class="na"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR_VIDEO_ID&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="na"&gt;snippet&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;newTitle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;//set title as new title&lt;/span&gt;
          &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;categoryId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;categoryId&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;   
       &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;},(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`There was an error updating &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Done&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can set the new title from the details collected from the &lt;code&gt;list&lt;/code&gt; function and use it with the update function. YouTube also wants us to set the Category ID of the video while updating the details. The complete list of category IDs can be found &lt;a href="https://gist.github.com/dgp/1b24bf2961521bd75d6c"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The update function can be called from inside the list function if you want to, to directly access the data in the response of the API.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Google Developer Console
&lt;/h2&gt;

&lt;p&gt;Now let's look at the &lt;code&gt;auth&lt;/code&gt; object that we pass in the function calls. &lt;code&gt;auth&lt;/code&gt; is an &lt;code&gt;oauth2client&lt;/code&gt; object that has our credentials and information like the access token and refresh token and all. &lt;/p&gt;

&lt;p&gt;Remember how when using the API explorer Google asked us to select an account and approve all the permissions? We generate access tokens and store it and pass it to our function calls so that we do not need to do this confirmation every single time the function is called. And since we need to update the title in real time, we will need to call the function frequently. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To generate the access tokens, first create a project in your &lt;a href="https://console.cloud.google.com"&gt;Google Developer Console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Add the YouTube Data API v3 to your project from the API library&lt;/li&gt;
&lt;li&gt;Go to the credentials tab and create a new &lt;code&gt;OAuth Client ID&lt;/code&gt; from the &lt;code&gt;Create Credential&lt;/code&gt; menu&lt;/li&gt;
&lt;li&gt;You will need to create an OAuth consent screen for this, which you can do easily from the side menu bar in the console.&lt;/li&gt;
&lt;li&gt;Once created, download the &lt;code&gt;clien_secret.json&lt;/code&gt; file and keep it in yoru project directory. Don't forget to add it to &lt;code&gt;.gitignore&lt;/code&gt; if you are going to push your code to a public repository.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;There are a few function that we need to run to generate the access token for the first time. This involves running the code from our local machine and confirming the use of the app we created with our Google account. &lt;/p&gt;

&lt;p&gt;For doing this, you can clone the repo I made &lt;a href="https://github.com/fillerInk/this-video-has-x-views"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After cloning, run &lt;code&gt;npm install&lt;/code&gt; inside the directory with the &lt;code&gt;package.json&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Copy the &lt;code&gt;client_secret.json&lt;/code&gt; file downloaded from your Google Developer Console into the project directory. &lt;/li&gt;
&lt;li&gt;Create a new micro inside the cloned project with &lt;code&gt;deta new&lt;/code&gt;. This will create a Micro for you based on the existing files. Once created you can go on and run &lt;code&gt;deta deploy&lt;/code&gt;, but we'll wait until everything is done to deploy it.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;npm start&lt;/code&gt; to start the local server on your machine. &lt;/li&gt;
&lt;li&gt;You will be given a url in the terminal. Go there and you will get a code in the address bar after you select your Google account and accept the terms. (If it says the app is not trustable, its because we haven't verified it. Don't worry, you are the only person who can access the app.)&lt;/li&gt;
&lt;li&gt;Paste this code back into your terminal and this will generate a directory named &lt;code&gt;.credentials&lt;/code&gt; in your project folder with your access tokens.&lt;/li&gt;
&lt;li&gt;Run
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deta deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to deploy your micro&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;deta cron &lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="s1"&gt;'5 minutes'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(or however frequent you want the code to run) from the terminal to set the cron&lt;/p&gt;




&lt;p&gt;Deta Cron will schedule the running of whatever piece of code you put inside &lt;code&gt;app.lib.cron(event =&amp;gt; { });&lt;/code&gt;. You can use it to automate whatever routine tasks that you might have. &lt;/p&gt;

&lt;p&gt;This blog post was meant to be a tutorial for a use-case for Deta Cron. Hope this gave you some insight into using Deta in your next project!&lt;/p&gt;

&lt;p&gt;Check out my GitHub repo for this project &lt;a href="https://github.com/fillerInk/this-video-has-x-views"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deta</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>BTW, I use Arch! 😂</title>
      <dc:creator>Sreeram Venkitesh</dc:creator>
      <pubDate>Thu, 09 Jul 2020 16:38:53 +0000</pubDate>
      <link>https://forem.com/fillerink/btw-i-use-arch-2nll</link>
      <guid>https://forem.com/fillerink/btw-i-use-arch-2nll</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fweawg96nbplroggqwmuc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fweawg96nbplroggqwmuc.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the story of how I installed Arch Linux. This is also the story of overcoming the challenges we face and why getting into sticky situations that get you scratching your head is actually a good thing.&lt;/p&gt;

&lt;p&gt;This post is actually based on &lt;a href="https://twitter.com/fillerInk/status/1206446000560193537" rel="noopener noreferrer"&gt;one of my Twitter threads&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So it had been a long time since I wanted to clean up my hard drive. I had been dual-booting with Windows alongside for design work and whatnot. Then one day I accidentally wiped the windows boot file and made it inaccessible. Needless to say my partition table was pretty messed up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fril4yaxz492v2rblsnrx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fril4yaxz492v2rblsnrx.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then a few days before Christmas 2019 (Ahh, good times), my desktop environment crashed. (I had been using Manjaro with Plasma for around the last 6 months) This was the best chance to clean my machine and start fresh for 2020 (Little did I know back then that 2020 would end up like this)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsd5pdddd4ahxe5ecfevw.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsd5pdddd4ahxe5ecfevw.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The boring part was copying all my files 🤦‍♂️ Spoiler alert - I found more it difficult than installing Arch. I had installed xfce as my makeshift DE for now.  I once deleted the copied folder thinking it was the older one. Luckily, I checked before deleting the original one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdghi0089rnqlcyb9bded.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdghi0089rnqlcyb9bded.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After copying the files, I decided to practice the installation process in a virtual machine first. I cannot mess this up right now :P So with a virtual machine set up, I set out to install Arch! The Arch Wiki is really cool if you have the patience to read it thoroughly!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkn7jbp0n8vzmn5ee88sm.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkn7jbp0n8vzmn5ee88sm.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After two tries, I got bare bones Arch installed! The first time it didn't install properly, second time I forgot to create the grub config and it booted into the grub command line, and the third time I finally got it. Exciting!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0abuza57i6ow0r0ezo4x.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0abuza57i6ow0r0ezo4x.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But I soon realised that I didn't enable dhcpcd and hence couldn't access the internet to download further packages or anything! Shucks!&lt;/p&gt;

&lt;p&gt;I tried a fourth time and this time, got everything correct and installed Plasma as my desktop environment 😁 (No wonder the K in KDE stood for Kool!) The satisfaction when you finally reach this point is really worth the effort.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzdjttanizpi7mos2ubai.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzdjttanizpi7mos2ubai.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And within two days after my DE first crashed, I started the installation in my actual hard drive, wiping all data and the messy mixture of windows and linux partitions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmcm6tay3c87ynefosnnw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmcm6tay3c87ynefosnnw.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Its good to have a book to read while it downloads the packages as it may take time..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb2889vby96ye5kfb0dhe.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb2889vby96ye5kfb0dhe.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally everything was installed perfectly! All the mistake I had made upto now made me sure to check for everything and it went smooth&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo8o6oqfnce7yfwnhju98.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fo8o6oqfnce7yfwnhju98.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this is how my drive looks now!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjjm16q46i6nsjqv83a83.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjjm16q46i6nsjqv83a83.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So why Arch? I had been using Manjaro for some time now and was really familiar with the pacman package manager. Also I wanted to take up something to challenge myself, hopefully learning something new and interesting in the process!&lt;/p&gt;

&lt;p&gt;In that regard, this has been a true nerd-out experience for me. &lt;/p&gt;

&lt;h2&gt;
  
  
  Now I can proudly say 'BTW, I use Arch'
&lt;/h2&gt;

</description>
      <category>linux</category>
      <category>archlinux</category>
    </item>
    <item>
      <title>Making prompts - an app that gets you out of writer's blocks using Reddit APIs and Deta</title>
      <dc:creator>Sreeram Venkitesh</dc:creator>
      <pubDate>Sun, 05 Jul 2020 19:21:59 +0000</pubDate>
      <link>https://forem.com/deta/making-prompts-an-app-that-gets-you-out-of-writer-s-blocks-using-reddit-apis-and-deta-3eo5</link>
      <guid>https://forem.com/deta/making-prompts-an-app-that-gets-you-out-of-writer-s-blocks-using-reddit-apis-and-deta-3eo5</guid>
      <description>&lt;p&gt;As developers we know best that sometimes the best way to avoid an idea block is to just start coding (Weeks of coding can save hours of planning, but that's for another story)&lt;/p&gt;

&lt;p&gt;Here we are talking about writer's blocks. Coming up with an enjoyable piece of literature can be as challenging as programming, sometimes even more so. &lt;/p&gt;

&lt;p&gt;Prompts (live at &lt;a href="http://prompts.deta.dev"&gt;prompts.deta.dev&lt;/a&gt;) is a simple app that I made for writers to exercise their brains hopefully recover from a writer's block. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bj0njhJO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i91ji01dzpokaqq8osnr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bj0njhJO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i91ji01dzpokaqq8osnr.png" alt="The sleek homepage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Prompts works by fetching story prompts from the &lt;a href="https://reddit.com/r/writingprompts"&gt;r/WritingPrompts&lt;/a&gt; subreddit and presenting them to you in a minimalistic editor where you can complete the rest of the story. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--19S_ze8D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u0auc47wj6wlwr436ayw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--19S_ze8D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u0auc47wj6wlwr436ayw.png" alt="Prompts editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are several options to spice up the challenge - you can set yourself a timer and see how much you can write in a fixed amount of time and also calculate your typing speed&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6tGvsBCo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rb3qjlbhop64ty0bmt2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6tGvsBCo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rb3qjlbhop64ty0bmt2r.png" alt="Timer and typing speed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you are done, you can keep the story you wrote for yourself by exporting it as a document. If you didn't like the prompt you got you can always load another prompt from the menu.&lt;/p&gt;




&lt;p&gt;Prompts is hosted with Deta Micros. Deta's Micros or Micro Servers lets you host your Python and Node.js apps in seconds with just their CLI tool. Check out more about them at &lt;a href="https://deta.sh"&gt;deta.sh&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code for prompts is open-source at my GitHub &lt;a href="https://github.com/fillerink/prompts"&gt;here&lt;/a&gt;. Any contributions are welcome! (If you are planning to run it locally, the instructions to set up your own Deta Micros is given in the repo's README.md file)&lt;/p&gt;

&lt;p&gt;Prompts is also featured in ProductHunt. You can check it out &lt;a href="https://producthunt.com/posts/prompts-2"&gt;here&lt;/a&gt; &lt;/p&gt;




&lt;p&gt;What do you think about prompts? What all features would you like to add to prompts? Share your feedback in the comments!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>deta</category>
    </item>
    <item>
      <title>How I built my quarantine project (Thanks to Node.js and Deta Base)</title>
      <dc:creator>Sreeram Venkitesh</dc:creator>
      <pubDate>Wed, 01 Jul 2020 09:38:20 +0000</pubDate>
      <link>https://forem.com/deta/how-i-built-my-quarantine-project-thanks-to-node-js-and-deta-base-50c9</link>
      <guid>https://forem.com/deta/how-i-built-my-quarantine-project-thanks-to-node-js-and-deta-base-50c9</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xOVjJdD3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o3wphrw7biz6p4ebtnnu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xOVjJdD3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o3wphrw7biz6p4ebtnnu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://deta.sh"&gt;Deta&lt;/a&gt; is a cloud platform or a cloud computer as devs call it, and it is built with developer and user experience as the first priority.&lt;/p&gt;

&lt;p&gt;Deta Base, the production grade NoSQL database provided by Deta is super easy to use that you can go from nothing to a fully working database for your project in literally minutes!&lt;/p&gt;

&lt;p&gt;This post is about how I used Base as the database for one of my side projects and how setting up Base was the easiest part of the project&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BeRl_TiL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/52hnt2ylznhvxjontvp5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BeRl_TiL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/52hnt2ylznhvxjontvp5.png" alt="The project I made"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;Once you create account at &lt;a href="https://deta.sh"&gt;deta.sh&lt;/a&gt;, you'll be taken to your dashboard and will be able to see your projects. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VoycDbqa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbj123d11gafcrsn777w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VoycDbqa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wbj123d11gafcrsn777w.png" alt="Deta Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you are first logging in, you'll have a default project with which you can play around. On opening the default project, you'll get a &lt;strong&gt;project key&lt;/strong&gt;. Copy this and save it somewhere safe because you won't get to see this again! &lt;/p&gt;

&lt;p&gt;Once you've got the project key setting up a database is just a few lines of code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Deta in your projects
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;As of now, you can use Deta Base with you Node.js or Python apps, or as an HTTP API&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Based on the stack you are using, you can install the relevant Deta packages -&lt;/p&gt;

&lt;p&gt;For Node.js, you can use npm to install Deta&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install deta&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Deta also has a Python SDK that you can get from &lt;a href="https://docs.deta.sh/docs/base/py_tutorial"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Base
&lt;/h2&gt;

&lt;p&gt;The app I was working on was a resume builder. The user could login and create a resume of all the things they did while being in quarantine or lockdown during the pandemic. &lt;/p&gt;

&lt;p&gt;For this I needed to store the data of the users once they login and their resumes once they create them. I did this by creating two Bases within the same project. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One awesome thing about Deta is that you can have infinite number of 'Bases' or databases within a single project&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For achieving this, I referred to the amazing &lt;a href="https://docs.deta.sh/docs/base/lib"&gt;docs&lt;/a&gt; and referring to the examples give there. &lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to create a Base and read/write data
&lt;/h3&gt;

&lt;p&gt;Setting up a Base is as simple as creating a deta object with your project key! You can start writing into and reading from your database right away. Here I'm showing how you can do it with Javascript&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; In your JavaScript file, first import the package
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Deta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;deta&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add your project key to a Deta object
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Deta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your_project_key&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now you can create a new database using deta.Base(). Let’s create a sample Base and write some values into it.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since I need to keep a record of all the users who sign up into my site, I need a users database to store their usernames and email addresses and so on.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I can easily write into the Base once I collect user infotmation from the frontend of my app.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;     
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sreeram&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abc@xyz.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Since Base is a flexible NoSQL database, we can store the data in whatever schema we need or by passing a JSON object we get from the user's input.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;put&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;     
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sreeram Venkitesh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abc@xyz.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;socialLinks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{...}],&lt;/span&gt;
  &lt;span class="na"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{...}],&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;resume1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing to note is that if you do not give a &lt;strong&gt;key&lt;/strong&gt; element manually, Deta will autogenerate a key for the entry. The key can be used to later retrieve the entry from the database. &lt;/p&gt;

&lt;p&gt;For my resume project, I used a string made from the email addresses of the users as the key so I can easliy retrieve the users' data from the db once I get the email addresses with which they are signing in the next time (See I told you, setting up Base was the easiest part of this project!)&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading from the Base
&lt;/h3&gt;

&lt;p&gt;So once I wrote all the code to retrieve the resume from the user and write it into the db, I created a custom link to the user's profile which they can share to show off their resume. &lt;/p&gt;

&lt;p&gt;In the backend of this process, I needed to get the user's unique key and search it in Base and retrieve the data stored there. &lt;/p&gt;

&lt;p&gt;I could do this easily with the db.get() function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user_resume&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user_key&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I can then easily use the returned object to display the data in the user's profile. &lt;/p&gt;




&lt;p&gt;&lt;a href="https://quarantineresu.me"&gt;quarantineresu.me&lt;/a&gt; was the first real Node.js application that I made. I literally had no idea how I could set up a database either. Deta made it really easy for me to get up going with the database so that I could concentrate on other aspects of building my app.&lt;/p&gt;

&lt;p&gt;Check out Deta at &lt;a href="https://twitter.com/detahq"&gt;Twitter&lt;/a&gt; or join the wonderful &lt;a href="https://deta-hq.slack.com/"&gt;Slack community&lt;/a&gt; to meet other developers or for help with coding! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>database</category>
    </item>
  </channel>
</rss>
