<?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>DEV Community: Akash R Chandran</title>
    <description>The latest articles on DEV Community by Akash R Chandran (@akashrchandran).</description>
    <link>https://dev.to/akashrchandran</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%2F945313%2F9c7a4869-6dc1-4ac6-9d0d-3c625e175f2f.jpg</url>
      <title>DEV Community: Akash R Chandran</title>
      <link>https://dev.to/akashrchandran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akashrchandran"/>
    <language>en</language>
    <item>
      <title>How to create REST API using Django REST framework</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Mon, 29 May 2023 11:55:47 +0000</pubDate>
      <link>https://dev.to/akashrchandran/how-to-create-rest-api-using-django-rest-framework-f4o</link>
      <guid>https://dev.to/akashrchandran/how-to-create-rest-api-using-django-rest-framework-f4o</guid>
      <description>&lt;p&gt;Let me introduce you to Steve, a banana farmer who cultivates a diverse range of bananas, from Apple bananas to Lady Fingers bananas. And steve love's his bananas and wants to sell them to the online market. Let's say he already has a website to list your bananas on. As of now static you have stock available hardcore written to HTML file. Every time he wants to update you have to edit the HTML file which is sad🤕. He finally decide to make the website dynamic by creating an API to get stock information from the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wXFk7Sg3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.giphy.com/media/cw8Nr4u28tVKw/giphy.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wXFk7Sg3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.giphy.com/media/cw8Nr4u28tVKw/giphy.webp" alt="a gif" width="480" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;He takes his Banana Book laptop and starts coding. He chooses Django paired with the Django REST framework as we want to make the banana website robust. But he gets get stuck and couldn't figure out what to do next. He reaches burnout and feels like he cannot do it. Then he remembers his bananas and his love for bananas gave him more determination. He remembers an old friend &lt;strong&gt;Akash😎&lt;/strong&gt;, who can help him to complete this task. So he asks the guy and that guy writes a tutorial for him.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's help Steve to make a REST API using the Django REST framework.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Let's help Steve
&lt;/h2&gt;

&lt;p&gt;Steve is a great guy and his love for bananas is immeasurable, so let's help Steve by building him a REST API using the Django REST framework. Firstly we require a laptop that has Python3 installed on it. You can download it from &lt;a href="http://Python.org"&gt;Python.org&lt;/a&gt;. Different versions of Python are available but be careful and choose any version &lt;code&gt;&amp;gt;= 3.8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will be building a Rest API to fetch banana stocks from the database. Let's not connect the database, it may get confusing to call it in one tutorial. Here instead we can use a JSON file which stores data and parse it to get the values of the stock. We are trying to learn how to use the Django rest framework and create a REST API using it. So we won't be learning much about database connection here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up Django
&lt;/h2&gt;

&lt;p&gt;It's better to use a dedicated environment for each Django project. Even Django team suggests using a virtual environment. Python itself comes with &lt;a href="https://docs.python.org/3/tutorial/venv.html"&gt;venv&lt;/a&gt; for managing environments which we will use for this guide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up environment
&lt;/h3&gt;

&lt;p&gt;You can give any name to the virtual environment, but here I am going with &lt;code&gt;djangoproject&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Windows&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Make sure you have installed Python before continuing to the next instructions.&lt;/p&gt;

&lt;p&gt;Next, create a folder in your preferred directory and name it whatever you like I am going with &lt;code&gt;Django REST API framework&lt;/code&gt;. Next to set up the virtual environment enter the command opening terminal or CMD.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;py &lt;span class="nt"&gt;-3&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; venv djangoproject
.venv&lt;span class="se"&gt;\s&lt;/span&gt;cripts&lt;span class="se"&gt;\a&lt;/span&gt;ctivate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Linux or Mac OS&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Most of the Linux Distributions come with python3 installed by default, just check if it exists by using the command.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For Mac users just head to &lt;a href="http://python.org"&gt;&lt;strong&gt;python.org&lt;/strong&gt;&lt;/a&gt; and download the .pkg file and start installing.&lt;/p&gt;

&lt;p&gt;Next to setting up a virtual environment, on Linux sometimes we need to install the extra library. To do the same, just enter the command in your terminal.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;python3-venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next, to initialize and activate a virtual environment on Mac and Linux, use the commands below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv djangoproject
&lt;span class="nb"&gt;source&lt;/span&gt; .venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Installing Django
&lt;/h3&gt;

&lt;p&gt;Next, we need to install Django on our virtual environment. To do that we can simply use the command below. Make sure you have activated the virtual environment before executing this command.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;Django
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After that, we have to start a project in our directory to get started. Django provides a template for the project and we can use that to create our app. To get the template in our current working directory you can use the command below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;django-admin startproject django_rest_trial &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;django_rest_trial&lt;/code&gt; is the name of the project which I choose, but you're free to use whatever name you prefer. After running that some files and folders should appear on the file explorer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---j0oY69b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685249151526/6c781684-9c75-4733-9dc8-b9dffadeda67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---j0oY69b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685249151526/6c781684-9c75-4733-9dc8-b9dffadeda67.png" alt="Vs code showing the files after running the Django start project command" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we need to create an app inside our Django project. The apps are like parts of a website which manage a certain task. for example, we can have an app for handling payment on the website or we can have an app that verifies login and sign-up. This is one of the pros of using Django as different teams can manage different apps yet they can make them work together.&lt;/p&gt;

&lt;p&gt;To create a Django app you can use the below command. Here &lt;code&gt;rest_api&lt;/code&gt; is my app name as it manages the REST APIs on our website. Feel free to choose a different name if you want to.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;py manage.py startapp rest_api &lt;span class="c"&gt;#windows&lt;/span&gt;

python3 manage.py startapp rest_api &lt;span class="c"&gt;# Linux and MAc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After executing this command a new folder should appear with the name of the app that was created using the above command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IvfFUQO9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685259633231/ff624e38-47c6-49a5-b098-e0a8508b8d05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IvfFUQO9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685259633231/ff624e38-47c6-49a5-b098-e0a8508b8d05.png" alt="Vs code showing the files after running the Django create app command" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even though we don't require a database, Django sessions and other values like admin, users etc. need a database. By default, Django comes with SQLite, which is a great option as it doesn't need much of any configuration. Django needs to migrate the required tables and values to the database to do that we have to use the below command.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;py manage.py migrate &lt;span class="c"&gt;# Windows&lt;/span&gt;

python manage.py migrate &lt;span class="c"&gt;# Linux&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We have finished setting up our Django project, you can even try running it by just running the server using the runserver command. Go to &lt;code&gt;http://127.0.0.1:8000/&lt;/code&gt; and you should see the standard Django greeting page.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;py manage.py runserver &lt;span class="c"&gt;# Windows&lt;/span&gt;

python3 manage.py runserver &lt;span class="c"&gt;# Linux&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zStNBa74--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685260149584/6b61a0ee-31cf-4b37-8375-07a73cad828b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zStNBa74--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685260149584/6b61a0ee-31cf-4b37-8375-07a73cad828b.png" alt="Standard django greeting page" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up the Django REST framework
&lt;/h2&gt;

&lt;p&gt;Next, we have to install the Django REST framework, to do the same we can use pip. To install it use the command below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;djangorestframework
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then we need to edit the &lt;code&gt;settings.py&lt;/code&gt; of our Django project and include &lt;code&gt;rest_framework&lt;/code&gt; in our project. You can find the settings file inside the main project folder which is the &lt;code&gt;django_rest_trial&lt;/code&gt; for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AaJ4RdUl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685260732247/28a9a1c4-a179-49bc-8ce9-81f3a177f77e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AaJ4RdUl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685260732247/28a9a1c4-a179-49bc-8ce9-81f3a177f77e.png" alt="Added rest_framework to the installed apps" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it we have completed the setup of the Django REST framework. We also need to install &lt;code&gt;whitenoise&lt;/code&gt; which helps in simplifying static file serving for Python web apps. You may think that we don't have any static files for our API, but Django has it for the admin panel, debug panel etc.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;whitenoise[brotli]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You have to set up &lt;code&gt;whitenoise&lt;/code&gt; as well, to do that you have to add &lt;code&gt;whitenoise&lt;/code&gt; to your middleware in the &lt;code&gt;setting.py&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;'django.middleware.security.SecurityMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'whitenoise.middleware.WhiteNoiseMiddleware'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;#Added
&lt;/span&gt;    &lt;span class="s"&gt;'django.contrib.sessions.middleware.SessionMiddleware'&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 need also setup static files root to that copy and paste the below line after &lt;code&gt;STATIC_URL&lt;/code&gt; variable. And add the lines below&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;DEBUG&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;STATIC_ROOT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'staticfiles'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;STATICFILES_STORAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'whitenoise.storage.CompressedManifestStaticFilesStorage'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Let's start coding
&lt;/h2&gt;

&lt;p&gt;First of all, we need to get the JSON file on which we are going to store the data on. I will provide it below, just copy and paste it into a file named &lt;code&gt;database.json&lt;/code&gt; and keep it the in the root folder with the &lt;code&gt;manage.py&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"bananaTypes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Apple Banana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Cavendish"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Lady Finger"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Red Dacca"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"availableStock"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next, we need to configure routes or endpoints which our REST APIs will use. Let the &lt;code&gt;rest_api&lt;/code&gt; app uses the route &lt;code&gt;/rest/&lt;/code&gt;. That is over API endpoints will be starting with the &lt;code&gt;rest&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;To configure this we need to create &lt;code&gt;urls.py&lt;/code&gt; file in the &lt;code&gt;rest_api&lt;/code&gt; app folder. Keeping it aside we will need it later, open the main project folder and open the urls.py file inside it and add the following element to the &lt;code&gt;urlpatterns&lt;/code&gt; list. You may need to import the &lt;code&gt;include&lt;/code&gt; function from the &lt;code&gt;django.urls&lt;/code&gt;. Finally, the file would look like this,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="c1"&gt;# include was also imported
&lt;/span&gt;
&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'admin/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'rest/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'rest_api.urls'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="c1"&gt;# added
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next, we will start coding the main part of the project. Open the &lt;code&gt;views.py&lt;/code&gt; file in the app folder and continue the part.&lt;/p&gt;
&lt;h3&gt;
  
  
  Coding the main part
&lt;/h3&gt;

&lt;p&gt;Let's start by creating a function that reads our database which is the &lt;code&gt;database.json&lt;/code&gt; file. I need to parse JSON so we import json library also. And create a &lt;code&gt;read_database()&lt;/code&gt; function. which somewhat looks like,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_database&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'database.json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After that, we create a function which accepts both &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; request and respond with the available stock of each banana variety. To do that we need to import &lt;code&gt;app_view&lt;/code&gt; and &lt;code&gt;Response&lt;/code&gt; from Django REST framework.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;app_view&lt;/code&gt; is a decorator which is used to denote what type of requests it can accept in our case we accept both &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; requests. It almost acts the same way as the &lt;code&gt;app.route&lt;/code&gt; in Flask, we mention methods in the same if you know.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Response&lt;/code&gt; object allows you to easily serialize data into the appropriate format (such as JSON or XML) and set the appropriate HTTP status code for the response. It also provides several convenient methods for setting headers, cookies, and other response metadata.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;views.py&lt;/code&gt; file would be like after all changes,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;rest_framework.decorators&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;api_view&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;rest_framework.response&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_database&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'daabase.json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;api_view&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s"&gt;'GET'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'POST'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_stocks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;read_database&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;banana_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'bananaTypes'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;avail_stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'availableStock'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;banana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;banana_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;avail_stock&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;banana&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'bananaStock'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s"&gt;'error'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you remember we have to still route the function &lt;code&gt;get_stocks&lt;/code&gt; to one of the endpoints. To do that open the &lt;code&gt;urls.py&lt;/code&gt; in the &lt;code&gt;rest_api&lt;/code&gt; or app folder and paste the below code,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;


&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'get_stock/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_stocks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'available stocks'&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 our Django REST API is successfully created. You can try running it on localhost by using the command &lt;code&gt;python manage.py runserver&lt;/code&gt; and heading to &lt;code&gt;http://127.0.0.1:8000/rest/get_stocks/&lt;/code&gt; to see the API working in real-time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XhFlgUaa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685292367383/68c4ba64-c1f6-45da-9448-7c5dd14e76e1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XhFlgUaa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685292367383/68c4ba64-c1f6-45da-9448-7c5dd14e76e1.png" alt="API response example" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can even use &lt;a href="https://www.postman.com/"&gt;Postman&lt;/a&gt; or &lt;a href="https://hoppscotch.io/"&gt;Hoppscotch&lt;/a&gt; to test the API to check if it's working correctly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XYCeZl33--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685292683521/f6a7682e-ce42-40fc-9d59-29d67e3252d4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XYCeZl33--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685292683521/f6a7682e-ce42-40fc-9d59-29d67e3252d4.png" alt="Testing API in Hoppscotch" width="800" height="484"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Hosting API on render.com
&lt;/h2&gt;

&lt;p&gt;You need an account on &lt;a href="https://www.render.com"&gt;render.com&lt;/a&gt;. You can signup for one &lt;a href="https://dashboard.render.com/register"&gt;here&lt;/a&gt;. And connect your GitHub account to it. You can find all the instructions in detail on my old blog, I will link it below&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/akashrchandran" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DrWNQW8X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--d83Caf3Y--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/945313/9c7a4869-6dc1-4ac6-9d0d-3c625e175f2f.jpg" alt="akashrchandran"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/akashrchandran/deploying-fastapi-application-to-render-47i1" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Deploying FastAPI application to Render&lt;/h2&gt;
      &lt;h3&gt;Akash R Chandran ・ Nov 2 '22 ・ 5 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#python&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Creating Requirements.txt&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Usually, I use external library &lt;a href="https://pypi.org/project/pipreqs/"&gt;&lt;strong&gt;pipreqs&lt;/strong&gt;&lt;/a&gt;, but you can just simply type &lt;code&gt;django&lt;/code&gt;, &lt;code&gt;djangorestframework&lt;/code&gt;, &lt;code&gt;gunicorn&lt;/code&gt; and &lt;code&gt;whitenoise[brotli]&lt;/code&gt; in the requirements.txt file. When deploying, render will install the latest version of these modules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y01rku5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685300911986/474cca7c-3367-497c-a941-c2b4bdce24c9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y01rku5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685300911986/474cca7c-3367-497c-a941-c2b4bdce24c9.png" alt="image showing requirements.txt file" width="775" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating a .gitignore file&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.gitignore&lt;/code&gt; file helps to avoid uploading unwanted or private files to the git clients such as GitHub or GitLab. When sharing your code with others, there are often files or parts of your project, you do not want to share. So these files or folders can be mentioned in the .gitignore file.&lt;/p&gt;

&lt;p&gt;For this Django project, we don't want to upload &lt;code&gt;djangoproject&lt;/code&gt;, &lt;code&gt;__pycache__&lt;/code&gt; and &lt;code&gt;db.sqlite3&lt;/code&gt;to GitHub. So we mention them in the .gitignore file like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xVXp8XjP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685294413376/178de7d6-60be-429b-9ae0-b5c8872b70c6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xVXp8XjP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685294413376/178de7d6-60be-429b-9ae0-b5c8872b70c6.png" alt="Image showing .gitignore file" width="625" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Upload to GitHub or GitLab&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;I am using GitHub here, but you can use GitLab as well. I have installed &lt;a href="https://git-scm.com/download/"&gt;&lt;strong&gt;git&lt;/strong&gt;&lt;/a&gt;, and I have connected it to my GitHub account. You'll find many articles on how to do it, just google it. If you're using Visual Studio then it has an built GitHub plugin which makes it way easier. I have successfully uploaded it to GitHub, you can find mine at&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9-wwsHG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/akashrchandran"&gt;
        akashrchandran
      &lt;/a&gt; / &lt;a href="https://github.com/akashrchandran/Django-REST-API-framework"&gt;
        Django-REST-API-framework
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      A example repository for my blog at https://blog.akashrchandran.in/how-to-create-rest-api-using-django-rest-framework.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
Django-REST-API-framework&lt;/h1&gt;
&lt;p&gt;A example repository for my blog at &lt;a href="https://blog.akashrchandran.in/how-to-create-rest-api-using-django-rest-framework" rel="nofollow"&gt;https://blog.akashrchandran.in/how-to-create-rest-api-using-django-rest-framework&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/akashrchandran/Django-REST-API-framework"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CPg8yD25--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685294694813/503bd37e-eab4-44d5-8253-3c1dfffc92b7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CPg8yD25--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685294694813/503bd37e-eab4-44d5-8253-3c1dfffc92b7.png" alt="Image showing my github repo" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Make the app production ready&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We will need to change some parts of the settings of Django to make it production ready.&lt;/p&gt;

&lt;p&gt;Find the declaration of the &lt;code&gt;SECRET_KEY&lt;/code&gt; setting by opening &lt;code&gt;settings.py&lt;/code&gt;. We don't want to keep production secrets in source code, so we'll get them via an environment variable we'll set later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Add this to begining of the file
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;os&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next change &lt;code&gt;SECRET_KEY&lt;/code&gt; to be equal to the environmental variable we are going to provide later.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'SECRET_KEY'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'thisisahugesecret'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Also, change &lt;code&gt;DEBUG&lt;/code&gt; to dynamically change based on environment variables&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'RENDER'&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We need to change allowed hosts also, as we are hosting. Rather than directly giving the host id after deployment we can also make it dynamic&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# https://docs.djangoproject.com/en/3.0/ref/settings/#allowed-hosts
&lt;/span&gt;&lt;span class="n"&gt;ALLOWED_HOSTS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="n"&gt;RENDER_EXTERNAL_HOSTNAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'RENDER_EXTERNAL_HOSTNAME'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;RENDER_EXTERNAL_HOSTNAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;ALLOWED_HOSTS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RENDER_EXTERNAL_HOSTNAME&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And we are done our project is now production ready. Now let's push changes to GitHub and continue.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Create a Build Script&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We need to run a series of commands to build our app. We can accomplish this with a build script. Create a script called &lt;code&gt;build.sh&lt;/code&gt; at the root of your repository:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# exit on error&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; errexit

pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt

python manage.py collectstatic &lt;span class="nt"&gt;--no-input&lt;/span&gt;
python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make sure the script is executable before checking it into Git:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;a+x build.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We completed the build script as well, and now push it to GitHub.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Deploying to&lt;/strong&gt; &lt;a href="http://Render.com"&gt;&lt;strong&gt;Render.com&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;As we are not deploying a static site, we should choose the option &lt;code&gt;Web Service&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XuI6A9no--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667302637873/HPSesfyBd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XuI6A9no--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667302637873/HPSesfyBd.png" alt="Choose web service" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have connected your GitHub account, then you can just search the repository name and find it there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tu9_var8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685299272521/671c9afc-1939-41ec-981c-d2abc7ffa1f6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tu9_var8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685299272521/671c9afc-1939-41ec-981c-d2abc7ffa1f6.png" alt="Select the repo by searching" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give a name to your web service, then select the region which is nearest to you, for me in Singapore. Leave all other values default. For the Build command, copy and paste the below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./build.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For the Start command, copy and paste the below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gunicorn django_rest_trial.wsgi:application
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;where &lt;code&gt;django_rest_trial&lt;/code&gt; is the name of the Django project. Do change this as per your project name.&lt;/p&gt;

&lt;p&gt;Next, scroll down and click the &lt;strong&gt;Advance&lt;/strong&gt; button and add the environment variables. Here we only require &lt;code&gt;SECRET_KEY&lt;/code&gt;. You can also use the generate button which is provided by render. After adding click the &lt;code&gt;Create Web Service&lt;/code&gt; button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bDTss-84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685299747963/6ce68d20-a7ec-4d08-a54f-ab9783c5c219.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bDTss-84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685299747963/6ce68d20-a7ec-4d08-a54f-ab9783c5c219.png" alt="Image showing build and start command" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After clicking the button you should see a log with events happening, wait till it finishes deploying.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YigsRFhr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685302806166/e96cd63d-a00b-460d-93de-343e24f58cea.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YigsRFhr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685302806166/e96cd63d-a00b-460d-93de-343e24f58cea.png" alt="Render deployment logs" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the site is live visit the route &lt;code&gt;/rest/get_stocks&lt;/code&gt; and you should see the API working.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fFe_wmRE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685302939368/e0dd92a6-3f01-4eac-9f9f-c32b670f8f37.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFe_wmRE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1685302939368/e0dd92a6-3f01-4eac-9f9f-c32b670f8f37.png" alt="Deployed version of our live site" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here we have done it and we have successfully deployed our REST API to render.com. Now sit back and relax by eating as many bananas as you can. The deployed version is available &lt;a href="https://django-rest-api-dzw1.onrender.com/rest/get_stocks/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Steve is partying
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CXQQydml--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/3KC2jD2QcBOSc/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CXQQydml--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/3KC2jD2QcBOSc/giphy.gif" alt="Party time" width="480" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for helping Steve to complete his REST API, he is now partying🥳. You can get more information on how to use the Django REST framework to best way read the &lt;a href="https://www.django-rest-framework.org/"&gt;documentation&lt;/a&gt;. There are many alternatives to the Django REST framework, you can give them a try if you want,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://django-tastypie.readthedocs.io/en/latest/"&gt;Django Tastypie&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://restless.readthedocs.io/en/latest/index.html"&gt;Django Restless&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/jsocol/django-jsonview"&gt;django-jsonview&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are good alternatives but some hardly maintained any more. Django REST framework is more powerful with authentication and stuff, you can learn about it in the docs.&lt;/p&gt;

&lt;p&gt;That's all we have completed the tutorial. Looking forward to meet you in the next one. Meanwhile you can follow me on dev and other platforms,&lt;/p&gt;


&lt;div class="ltag__user ltag__user__id__945313"&gt;
    &lt;a href="/akashrchandran" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DrWNQW8X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--d83Caf3Y--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/945313/9c7a4869-6dc1-4ac6-9d0d-3c625e175f2f.jpg" alt="akashrchandran image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/akashrchandran"&gt;Akash R Chandran&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/akashrchandran"&gt;A python developer who likes automation, creating bots and writing blogs. I also know other languages like JavaScript, Java, Dart etc.&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;Thanks for reading patiently.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Vercel Launches 3 New Storage Options for Your Next Project!</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Tue, 16 May 2023 13:44:21 +0000</pubDate>
      <link>https://dev.to/akashrchandran/vercel-launches-3-new-storage-options-for-your-next-project-29ok</link>
      <guid>https://dev.to/akashrchandran/vercel-launches-3-new-storage-options-for-your-next-project-29ok</guid>
      <description>&lt;p&gt;If you have tried manually deploying apps to production, then you may know how nerve-wracking things can be. Even the slightest mistake could cause the production database to be deleted, leading the company to lose billions of dollars, making you freak out and flee the country before your boss finds out, and start living in the Himalayas as a goat farmer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cd3x-Ro6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExODlhZjk3YWQzZGUzYzU2MGU0YzA1NjRiMzliYTIyYTNjOWU3Y2JjOCZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/3o7TKFy1gaoy1JNqZG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cd3x-Ro6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExODlhZjk3YWQzZGUzYzU2MGU0YzA1NjRiMzliYTIyYTNjOWU3Y2JjOCZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/3o7TKFy1gaoy1JNqZG/giphy.gif" alt="A simple gif" width="400" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's why most of the developers use Vercel, Netlify, Heroku, and Firebase as they handle deployment for you and prevent you from making any big mistakes. Anyways vercel just introduced 3 new databases or storage options. And let's see them in brief.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is vercel?
&lt;/h2&gt;

&lt;p&gt;Well, Vercel is a cloud platform which enables developers to host their websites and web apps that deploy and scale automatically. Vercel is the favourite choice for the deployment of Next.js apps as they have more efficient support for the framework. That's not that big as vercel also maintains the Next.js.&lt;/p&gt;

&lt;p&gt;But the best part is that they support almost every language framework though they specialize in javascript frameworks. I have hosted many Python frameworks like Flask, FastAPI and even Django. They even support PHP, I have hosted my app there and haven't got any issues with speed or rendering.&lt;/p&gt;

&lt;p&gt;They have a crazy awesome user interface and experience that makes it so user-friendly. It provides a free tier named &lt;code&gt;Hobby&lt;/code&gt; for developers to experiment and try their platform. Its against Vercels TOS to run a commercial app on the free tier. Even though you technically could, they will shut down your site if caught in violation. So you'll have to go with a paid plan and it's a bit expensive when compared to other platforms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Vercel introduced 3 new additions to their storage solutions. They did it on 1st May and you can find the announcement on the &lt;a href="https://vercel.com/blog/vercel-storage"&gt;website&lt;/a&gt;. The new additions are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://vercel.com/blog/vercel-storage#vercel-kv-a-durable-redis-database"&gt;Vercel KV&lt;/a&gt; (Beta)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://vercel.com/blog/vercel-storage#vercel-postgres-complex-data-made-easy"&gt;Vercel Postgres&lt;/a&gt; (Beta)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://vercel.com/blog/vercel-storage#vercel-blob-easy-file-storage-at-the-edge"&gt;Vercel Blob&lt;/a&gt; (Private Beta)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will be talking about them individually one by one below. This is a huge thing as they are trying to control users from using any other third-party database solution and managing it themselves. The database was the only thing missing on vercel but now it's kind of complete at least it is in beta for public use. They are not yet available for enterprise usage, mostly they will be available soon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=gA8cHj3w5XI&amp;amp;ab_channel=Vercel"&gt;https://www.youtube.com/watch?v=gA8cHj3w5XI&amp;amp;ab_channel=Vercel&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Vercel KV
&lt;/h2&gt;

&lt;p&gt;It's key-value storage like Redis, in fact exactly like Redis as it is powered by Upstash. If you didn't know Upstash is a &lt;strong&gt;Serverless in-memory cloud database&lt;/strong&gt; from Upstash Inc, a California-based company. It is a Serverless Redis Service. It can be used for caching layer or as a database. You can more details about it &lt;a href="https://vercel.com/storage/kv"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fGZtaEUp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684212956158/c38839ce-7fb7-4db3-bff4-69e01b854ab8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fGZtaEUp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684212956158/c38839ce-7fb7-4db3-bff4-69e01b854ab8.gif" alt="oh and it's really fast" width="360" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For now, I am more interested in the speed at which it works, let's try to create a simple free tier vercel KV storage. The creation is pretty straightforward, they just ask you for the name and the region. I went with Singapore as my region, as it seems to be the nearest one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KUNV6Kea--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684177607628/ee420bfe-7b58-49fc-937d-8425f1343e0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KUNV6Kea--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684177607628/ee420bfe-7b58-49fc-937d-8425f1343e0e.png" alt="creating vercel kv" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;They also provide CLI to set and get values directly from the database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I copied the Template that they provide for the testing, you can get the template source on &lt;a href="https://github.com/vercel/examples/tree/main/storage/kv-redis-starter"&gt;GitHub&lt;/a&gt;. I modified its &lt;code&gt;page.tsx&lt;/code&gt; file just to set and get a value from the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import kv from "@vercel/kv"

export default async function Home() {
  console.time("res");
  await kv.set("message", "Hello World!");
  const msg = await kv.get("message");
  console.timeEnd("res");
  return &amp;lt;p&amp;gt;{msg}&amp;lt;/p&amp;gt;;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I first ran it took more than &lt;code&gt;200 ms&lt;/code&gt; to load, as it was the first request server took time to warm up and respond. Even 2nd and 3rd try still resulted in more than &lt;code&gt;100 ms&lt;/code&gt;, but after that, it came down under to &lt;code&gt;25 - 50 ms&lt;/code&gt; range.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Keep in mind that this was both reading and writing. But when I tested reading from the database alone it was a little above &lt;code&gt;15 ms&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The storage supports all the operations that can be done on Redis. There are limitations applied on creation, requests, record size etc you can find them &lt;a href="https://vercel.com/docs/storage/vercel-kv/limits"&gt;here&lt;/a&gt;. The pricing seems to be a little above the expected but it may drop in the future.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vercel.com/docs/storage/vercel-kv/usage-and-pricing"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mFfX9HPh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684180919754/388f1031-64dc-4ed3-806e-48002bf7b539.png" alt="pricing" width="800" height="636"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Vercel Postgres
&lt;/h2&gt;

&lt;p&gt;This was the part for which I was most excited. They introduced Postgres one of the most advanced general-purpose object-relational database management systems. I have been using it for my projects. It's powered by Neon a fully managed serverless PostgreSQL. Neon separates storage and computing to offer modern developer features such as serverless, branching, bottomless storage, and more.&lt;/p&gt;

&lt;p&gt;Coincidentally I used Neon for a freelancing project and it was amazing as it provide a web interface to edit, give analytics and view tables which made the development of my Django app super easy. I will have to try it to be sure if it is as good as Neon. The creation of Postgres is simply the same as that of Vercel KV and only requires a name and region ( Singapore is available so I choose that ).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DhRGX_B5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684213441471/4143005e-18af-489d-87cb-2cef682a196d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DhRGX_B5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684213441471/4143005e-18af-489d-87cb-2cef682a196d.png" alt="creating vercel postgres" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I copied the Template that they provide for the testing, you can get the template source at &lt;a href="https://github.com/vercel/examples/tree/main/storage/postgres-starter"&gt;GitHub&lt;/a&gt;. I modified its &lt;code&gt;page.tsx&lt;/code&gt; file just to create, insert and select a value from the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { sql } from "@vercel/postgres";

export default async function Home() {
  console.time("create");
  await sql`CREATE TABLE IF NOT EXISTS PERSON (name varchar(255), age int)`;
  console.timeEnd("insert");
  console.time("create");
  await sql`INSERT INTO PERSON (name, age) VALUES ('Akash', 21)`;
  console.timeEnd("insert");
  console.time("select");
  const { rows } = await sql`SELECT * FROM PERSON`;
  console.timeEnd("select");
  return (
    &amp;lt;div&amp;gt;
      {rows.map((row) =&amp;gt; (
        &amp;lt;div key={row.age}&amp;gt;
          {row.name} - {row.age}
        &amp;lt;/div&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  );
}


create: 474.285ms
insert: 102.999ms
select: 103.254ms

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have tested it for like 10 times and I think the time averages out nearest to these values. It's pretty good and fast for me at least. Vercel also has a web interface to view tables and execute queries same as Neon.&lt;/p&gt;

&lt;p&gt;The pricing is almost the same as that of Vercel KV. And also include the same limitations as of the same. You can view the pricing &lt;a href="https://vercel.com/docs/storage/vercel-postgres/usage-and-pricing"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vercel Blob
&lt;/h2&gt;

&lt;p&gt;This is something that can be very useful. Vercel Blob allows you to upload and serve files via a global network through unique and unguessable URLs. it's based on Cloudflare R2 which &lt;strong&gt;allows you to store large amounts of&lt;/strong&gt; unstructured data like images and PDF files on the edge in other words it's a replacement for storage buckets like S3 and that could be a game changer if your &lt;strong&gt;app does file uploads.&lt;/strong&gt; It is still in private beta but you can sign up for the waiting list to try it. To signup head to this &lt;a href="https://vercel.com/storage/blob"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;They say it is users can upload files of &lt;strong&gt;any size&lt;/strong&gt;. But for the private beta phase, the upload size is limited to &lt;code&gt;4 MB&lt;/code&gt;, but soon they will release its full potential and will be available for public beta.&lt;/p&gt;

&lt;p&gt;The SDK is pretty simple for developers to use same as the other storage. I don't have access to it so I can't test it for myself but I would say it would be good as the other two. As per the doc, the snippet given is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { put } from '@vercel/blob';
import { NextResponse } from 'next/server';

export default async function upload(request) {
  const form = await request.formData();
  const file = form.get('file') as File;
  const blob = await put(file.name, file, { access: 'public' });

  return NextResponse.json(blob);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pricing is available &lt;a href="https://vercel.com/docs/storage/vercel-blob/usage-and-pricing"&gt;here&lt;/a&gt;. It will be good for companies looking for storage that has some simplicity yet advanced technologies and have loads of money to waste.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is it worth it?
&lt;/h2&gt;

&lt;p&gt;Without much of a surprise No, it's not worth it as they are priced insanely high. As it's vercel they have a bit high pricing as always but the products are also somehow astonishingly good and have a good user interface. To phrase it they are Apple of the web industry. But that doesn't justify the pricing,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Vercel KV is 1.5x the price of Upstash scaling without the way more generous free tier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vercel Postgres is 2x the price of Neon scaling without the way more generous free tier. The free tier of Neon has 3GB of storage compared to 256MB of Vercel, no limit of storage on its pro plans compared to 512MB of storage for Vercel, no limit on read and write data compared to Vercel, no limit on compute time compared to Vercel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vercel Blob is 2 times the price of Cloudflare R2 without the way more generous free tier.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The prices may change later. The VP of Developer Experience at Vercel commented on Reddit that the prices may change with the feedback of the beta users and the pricing of vercel blob isn't final yet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tac4QIJ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684227309553/a9b66bea-32e7-41cb-9447-37f4d7004aad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tac4QIJ0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1684227309553/a9b66bea-32e7-41cb-9447-37f4d7004aad.png" alt="reddit comment" width="800" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is still good for developers who don't want to use other applications just for the database instead they can find all the things in a single place which makes it easier to maintain and use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Vercel is an amazing platform for hosting web applications without a doubt. And I am sure I am going to use Vercel Postgres for future projects for sure. You should try it for sure as all the storage options are available for the Hobby plan. Vercel is trying new &lt;a href="https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#server-actions"&gt;Server Actions&lt;/a&gt; an &lt;strong&gt;alpha&lt;/strong&gt; feature in Next.js, built on top of React &lt;a href="https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#actions"&gt;Actions&lt;/a&gt;. They enable server-side data mutations, reduced client-side JavaScript, and progressively enhanced forms. This would be a great feature for web developers.&lt;/p&gt;

&lt;p&gt;Lastly thank you for reading this patiently and please follow me on Linkedin, you can find the link at &lt;a href="https://www.linkedin.com/in/akashrchandran/"&gt;Akash R. Chandran | LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>database</category>
      <category>vercel</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to host FastAPI application on Deta Space</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Tue, 28 Mar 2023 02:30:39 +0000</pubDate>
      <link>https://dev.to/akashrchandran/how-to-host-fastapi-application-on-deta-space-3c7l</link>
      <guid>https://dev.to/akashrchandran/how-to-host-fastapi-application-on-deta-space-3c7l</guid>
      <description>&lt;p&gt;Almost 2 years ago while browsing through the FastAPI docs, I came across their sponsors one of which was &lt;a href="https://deta.sh"&gt;deta.sh&lt;/a&gt; and it is still there you can view it at &lt;a href="https://fastapi.tiangolo.com/#sponsors"&gt;FastAPI Docs&lt;/a&gt;. As a curious person, I opened the link and browsed through their website. At that time they had a minimalist design.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3B_l7aPU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://resource.fyi/_next/image%3Furl%3Dhttp%253A%252F%252Fres.cloudinary.com%252Fdnr4p6zlc%252Fimage%252Fupload%252Fv1666362295%252Fraimcg6gvhi7ydemcfb2.png%26w%3D3840%26q%3D75" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3B_l7aPU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://resource.fyi/_next/image%3Furl%3Dhttp%253A%252F%252Fres.cloudinary.com%252Fdnr4p6zlc%252Fimage%252Fupload%252Fv1666362295%252Fraimcg6gvhi7ydemcfb2.png%26w%3D3840%26q%3D75" alt="old sample view of deta.sh website" width="800" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They didn't have a GUI to manage apps only CLI to deploy and manipulate apps, the only thing they provided was the register/login and docs if I remember correctly. I tried deploying some apps but failed for some I don't remember. So let the deta cloud and tried something else.&lt;/p&gt;

&lt;p&gt;But yesterday I came across the same site and was amazed that they are shifting to a new better version called &lt;a href="https://deta.space/"&gt;Deta Space&lt;/a&gt;. This is in still beta but it has everything a modern platform has like GUI to manage apps, view logs, better docs and even a page to find user-build apps that are deployed on Deta Space. So I decided to test it out by deploying a sample FastAPI application. We will use the same repository as I used in my old blog showing the deployment of the FastAPI application on Render.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.akashrchandran.in/deploying-fastapi-application-to-render"&gt;https://blog.akashrchandran.in/deploying-fastapi-application-to-render&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Creating&lt;/strong&gt; &lt;a href="https://deta.space/"&gt;&lt;strong&gt;Deta Space&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;Account&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As with any other site, you will need an account to host apps on &lt;a href="https://deta.space/"&gt;Deta Space&lt;/a&gt;. They don't provide any third-party signup method so you will have to have to use a good old email password. You are required to choose a unique username, I think it's for subdomain assignment not sure yet though. You can sign up using this &lt;a href="https://deta.space/signup"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R1F6MXIa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679927384583/500d1f86-fc05-4761-92f0-77ef17d23df4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R1F6MXIa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679927384583/500d1f86-fc05-4761-92f0-77ef17d23df4.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After login, you should be redirected to the dashboard and you should look something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PiYaRwwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679928791396/143ca38c-9705-447c-8e47-0f5734d9edb9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PiYaRwwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679928791396/143ca38c-9705-447c-8e47-0f5734d9edb9.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up CLI
&lt;/h2&gt;

&lt;p&gt;Although the site has more GUI-rich features it still lacks a deployment option. So we have to set up our CLI tool if we want to continue. &lt;a href="https://deta.space/docs/en/basics/cli"&gt;Setting up the CLI - Space Docs (&lt;/a&gt;deta.space&lt;a href="https://deta.space/docs/en/basics/cli"&gt;)&lt;/a&gt; here you can find the instructions to do so. After installation login can be done by using the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;space login

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If installed correctly it should show something like this. Next, we need is the authentication token from Deta space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dwf9cz_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679936113776/cde33a31-7579-4afb-9172-4ff4a81d9bc0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dwf9cz_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679936113776/cde33a31-7579-4afb-9172-4ff4a81d9bc0.png" alt="image" width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting token from Deta Space
&lt;/h3&gt;

&lt;p&gt;After you log in you will be redirected to the dashboard, use the command palette. Just search for the settings and then click generate a new token.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9LgDXZiP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679936640983/cbad93e1-1d2f-49a7-a212-e180aca37879.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9LgDXZiP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679936640983/cbad93e1-1d2f-49a7-a212-e180aca37879.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PkORZr3D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679936700259/99f1a980-b930-4a7b-b9f1-24308c95df83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PkORZr3D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679936700259/99f1a980-b930-4a7b-b9f1-24308c95df83.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that copy the token and come back to the terminal page. And paste it onto the terminal&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_DKuoUJ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679937233478/3ba11078-f563-4576-a96e-25a7e7f794ed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_DKuoUJ---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679937233478/3ba11078-f563-4576-a96e-25a7e7f794ed.png" alt="image" width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we have successfully set up CLI. Now let's move on to the next part which is pushing the code to Deta Space.&lt;/p&gt;

&lt;p&gt;I am not going to show to how to code or install FastAPI for that you can read my old blog post.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.akashrchandran.in/deploying-fastapi-application-to-render#heading-lets-make-a-simple-api-using-fastapi"&gt;https://blog.akashrchandran.in/deploying-fastapi-application-to-render#heading-lets-make-a-simple-api-using-fastapi&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also view the code project at my GitHub repository at &lt;a href="https://github.com/akashrchandran/simple-api"&gt;akashrchandran/simple-api (&lt;/a&gt;&lt;a href="http://github.com"&gt;github.com&lt;/a&gt;&lt;a href="https://github.com/akashrchandran/simple-api"&gt;)&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating .spaceignore file
&lt;/h2&gt;

&lt;p&gt;To exclude certain files and directories from being uploaded during space push, use the .spaceignore file. It acts similarly to a .gitignore file, where each line in the file specifies a pattern. All files and directories matching the patterns will be excluded from the push.&lt;br&gt;&lt;br&gt;
For example, a &lt;code&gt;.spaceignore&lt;/code&gt; file with the following content &lt;code&gt;space push&lt;/code&gt; will ignore the &lt;code&gt;test&lt;/code&gt; and &lt;code&gt;docs&lt;/code&gt; paths.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test
docs

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For our project, we will be ignoring the folders&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;venv
__pycache__

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Deploying to Deta Space&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After all the hard work we have done here is going to pay off, let's make the app accessible from anywhere on the planet. Open up the code in visual studio code or any other IDE or Notepad your wish. And open the terminal on the side or use the integrated terminal option in VS code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GUbD44Tp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679940982983/7d575395-2e0d-4c04-a848-2164bec1fef4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GUbD44Tp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679940982983/7d575395-2e0d-4c04-a848-2164bec1fef4.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Make sure you have the Space CLI installed &amp;amp; authenticated, then run the following command to create a new Space project in the current directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;space new

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iIlLTk5D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679941199101/c86860d0-967b-4e58-9d7e-5740eceea9da.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iIlLTk5D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679941199101/c86860d0-967b-4e58-9d7e-5740eceea9da.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Editing the Spacefile
&lt;/h3&gt;

&lt;p&gt;This will create a &lt;code&gt;Spacefile&lt;/code&gt; in the root directory. And it will only have the version &lt;code&gt;v:0&lt;/code&gt; as text and comment to the docs. This is where we define our runtime engine and other requirements. To get a clear idea about it you can visit the &lt;a href="https://deta.space/docs/en/reference/spacefile/"&gt;Spacefile - Space Docs (&lt;/a&gt;&lt;a href="http://deta.space"&gt;deta.space&lt;/a&gt;&lt;a href="https://deta.space/docs/en/reference/spacefile/"&gt;)&lt;/a&gt;. To run the FastAPI app we need to define some requirements in the Spacefile. Here's what the file looks like after adding the required lines.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qVHLRH6E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679941690567/ea9cafee-b456-4015-af20-b0101492e191.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qVHLRH6E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679941690567/ea9cafee-b456-4015-af20-b0101492e191.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
icon: ./icon.png
app_name: "Simple FastAPI APP"
micros:
  - name: simple-fastapi
    src: ./
    engine: python3.9
    dev: .venv/bin/uvicorn main:app --reload

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran into a few troubles while making the Spacefile, it seems python apps don't support &lt;code&gt;run&lt;/code&gt; commands. Use &lt;code&gt;dev&lt;/code&gt; to specify a command which starts your Micro in &lt;strong&gt;development&lt;/strong&gt; mode. This command will be used to start your micro when you run.&lt;br&gt;&lt;br&gt;
You can specify the version of python you need using the key &lt;code&gt;engine&lt;/code&gt;. There are only two versions available currently &lt;code&gt;python3.9&lt;/code&gt; and &lt;code&gt;python3.8&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;code&gt;src&lt;/code&gt; is the directory where the main.py file resides. You can keep it inside another folder and then specify the path to that folder.&lt;/p&gt;

&lt;p&gt;for the &lt;code&gt;icon&lt;/code&gt; I am using the logo of FastAPI, you can check it out at &lt;a href="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"&gt;logo-teal.png (1023369) (&lt;/a&gt;&lt;a href="http://tiangolo.com"&gt;tiangolo.com&lt;/a&gt;&lt;a href="https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"&gt;)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; You are required to keep the file main.py as the entry file or it will not work. Make sure you use &lt;code&gt;main.py&lt;/code&gt; for any python project.&lt;/p&gt;
&lt;h3&gt;
  
  
  Deploying the app
&lt;/h3&gt;

&lt;p&gt;To deploy we just need to give the command, and it should automatically deploy and give the output URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;space push

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_dNxNKUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkcesl2j4i81nnazh9di.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_dNxNKUs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkcesl2j4i81nnazh9di.gif" alt="gif" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations the app is live🥳🥳. And you will be able to see the application in your dashboard. You can find my deployed version at &lt;a href="https://simplefastapi-1-h6036723.deta.app"&gt;https://simplefastapi-1-h6036723.deta.app&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BxrLMOOD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679943882001/786030ad-54a9-49a0-8b72-7da06d2f5b0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BxrLMOOD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1679943882001/786030ad-54a9-49a0-8b72-7da06d2f5b0a.png" alt="image" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This was a simple blog explaining the way to deploy the FastAPI application on Deta Space. There are many more things you can add to the project, I am just setting an example. The documentation is pretty amazing and you can find almost all the parts documented, do give it a read at &lt;a href="https://deta.space/docs/en/introduction/start"&gt;Getting started - Space Docs (&lt;/a&gt;&lt;a href="http://deta.space"&gt;deta.space&lt;/a&gt;&lt;a href="https://deta.space/docs/en/introduction/start"&gt;)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Overall I would say it's pretty easy compared to other services, even though some services are popular yet it's hard to get the app deployed on the service. The Deta Space is not that famous but it was easy to deploy apps. I am sure that I will use the service again to host my apps.&lt;/p&gt;

&lt;p&gt;The Deta Space is still under construction and it's developed and updated daily. This makes it vulnerable to bugs and other problems. If you are stuck at some place, first read the docs. If you're still stuck after that then you can join their Discord server and post your queries and problems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://discord.com/invite/K3gYNqHXUU"&gt;https://discord.com/invite/K3gYNqHXUU&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The community may be small but they are active and almost answers every single query raised. Giving feedback will help build the platform faster and better, do share your review on Discord.&lt;/p&gt;

&lt;p&gt;Finally thank you for reading this blog, it was a good experience to share my view and knowledge about the platform. Have a great day!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>fastapi</category>
    </item>
    <item>
      <title>Top 10 Python Packages Every Developer Should Know About</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Fri, 10 Mar 2023 17:04:08 +0000</pubDate>
      <link>https://dev.to/akashrchandran/top-10-python-packages-every-developer-should-know-about-55mj</link>
      <guid>https://dev.to/akashrchandran/top-10-python-packages-every-developer-should-know-about-55mj</guid>
      <description>&lt;p&gt;Python is undoubtedly one of the most popular programming languages in today's tech industry. With an ever-increasing number of developers turning to it, Python has established itself as a must-know skill for any aspiring programmer or software engineer. One reason behind its massive popularity is the vast collection of python packages that make coding more efficient and seamless. If you're looking to take your Python development skills to the next level, we've got you covered. In this post, we'll be discussing ten essential python packages every developer should know about to boost their productivity and streamline their workflow. Whether you're building web applications, performing data analysis tasks or developing machine learning models, these packages are guaranteed to simplify your work and enhance your code quality. So buckle up and get ready to dive into our top 10 list!&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Python Packages
&lt;/h2&gt;

&lt;p&gt;Python is a widely used high-level interpreted language. Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.&lt;/p&gt;

&lt;p&gt;Python packages are collections of modules that you can import into your scripts to make use of their functionality. In this article, we'll take a look at some of the most popular Python packages that every developer should know about.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Python Package?
&lt;/h2&gt;

&lt;p&gt;Python packages are modules that contain code written in the Python programming language. Packages can be used to extend the functionality of Python or to add new features to existing Python code. Python packages can be installed from a variety of sources, including the Python Package Index (PyPI), or third-party repositories. Once installed, packages can be imported into your Python code using the import statement.&lt;/p&gt;

&lt;p&gt;Packages can contain a variety of different types of content, including:&lt;/p&gt;

&lt;p&gt;Modules: A module is a single Python file that contains code. Modules can be imported into other modules, or the main Python interpreter, using the import statement.&lt;/p&gt;

&lt;p&gt;Classes: A class is a template for creating objects. Classes can be defined in modules and then imported into other modules, or the main Python interpreter, using the from...import statement.&lt;/p&gt;

&lt;p&gt;Functions: A function is a block of code that performs a specific task. Functions can be defined in modules and then imported into other modules, or the main Python interpreter, using the from...import statement.&lt;/p&gt;

&lt;p&gt;Data: Data is any information that can be stored in computer memory. Data can be stored in variables, and passed to functions as arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Packages are:
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pandas
&lt;/h3&gt;

&lt;p&gt;The Python package Pandas is a free and open-source library that provides high-performance data structures and data analysis tools. It is built on the popular Python libraries NumPy and matplotlib, which allow for efficient data manipulation and analysis. Data structures and operations for manipulating numerical tables and time series data are available in Pandas. It is a must-have for data manipulation and data science tasks. Pandas is a highly effective data analysis, manipulation, and visualisation tool. It is used in many industries, including finance, analytics, and data science, for data cleaning, preparation, and analysis. Pandas make it simple to work with large datasets, allowing users to summarise, manipulate, and visualise data quickly and easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  NumPy
&lt;/h3&gt;

&lt;p&gt;NumPy is a robust and effective module for doing scientific computations. It offers quick mathematical operations on these massive, multi-dimensional arrays and matrices for which it is developed. Many mathematical operations are offered by NumPy, such as Fourier analysis, linear algebra, and random number creation. For manipulating arrays, it also offers sophisticated indexing and slicing features. In disciplines including data science, machine learning, and computational physics, NumPy is extensively employed. Its popularity is a result of the performance and usability it combines, making it a crucial Python tool for scientific computing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Matplotlib
&lt;/h3&gt;

&lt;p&gt;Matplotlib is a Python package that offers a range of tools to developers for making interactive visualisations, such as graphs and charts, that can be used for data analysis. It is a commonly used library in science, and it works particularly well for making static 2D and 3D plots that may be highly customised and detailed. Also, it offers assistance with story interactivity and animation creation. After you grasp the fundamentals of charting, Matplotlib's simple syntax makes it simple to use. For everyone who wishes to extract amazing images from their data, it offers a necessary tool.&lt;/p&gt;

&lt;h3&gt;
  
  
  TensorFlow
&lt;/h3&gt;

&lt;p&gt;Popular open-source software called TensorFlow is used to create and train machine learning models. It was created by Google and is one of the most used deep learning frameworks. Because TensorFlow is built on a dataflow graph, users can see how data flows through a neural network model. It offers a versatile and effective language for creating and refining models on huge datasets. Applications supported by this package include natural language processing, picture and speech recognition, and more. You may quickly scale sophisticated models to huge datasets with TensorFlow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scikit-learn
&lt;/h3&gt;

&lt;p&gt;Built on top of NumPy, SciPy, and matplotlib, Scikit-learn is a free and open-source machine learning framework. It offers straightforward and effective tools for data analysis and mining. Scikit-learn offers tools for model selection and evaluation, data preprocessing, feature selection, and model visualisation in addition to several algorithms for classification, regression, clustering, and dimensionality reduction. Classification, regression, natural language processing, image processing, and other machine learning tasks are only a few of the many machine learning tasks for which it is frequently employed in both industry and research. Scikit-learn is a vital tool for anyone interested in machine learning because of its simplicity of use and extensive capability.&lt;/p&gt;

&lt;h3&gt;
  
  
  PyTorch
&lt;/h3&gt;

&lt;p&gt;A well-liked open-source machine-learning library for Python is called Pytorch. Because of its adaptability, clarity, and scalability, it is widely used. Using the capabilities of graphics processing units (GPUs) to speed up training, Pytorch offers a simple interface for creating deep learning algorithms. The software is widely used in research, education, and business for a range of tasks including speech recognition, computer vision, natural language processing, and more. Pytorch has developed into a vital toolkit for machine learning enthusiasts and engineers alike thanks to its comprehensive collection of tools and community support.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flask
&lt;/h3&gt;

&lt;p&gt;Flask is a lightweight and powerful web development framework for building Python-based web applications. It includes a comprehensive set of tools and libraries for developing a wide range of web applications, from simple static sites to more complex web applications with dynamic data. Flask has an easy-to-learn and-use API, as well as a flexible and extensible design that allows you to tailor it to your specific requirements. It also includes built-in unit testing support, simple error handling, and easy integration with other libraries and services. Because of its simplicity, flexibility, and ease of use, Flask is a popular choice for developing web applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Django
&lt;/h3&gt;

&lt;p&gt;Django is a Python web framework that is open source and follows the Model-View-Controller (MVC) architecture. It is widely used to rapidly create high-quality web applications. Django's clean design encourages code reuse and employs object-oriented programming principles. Django includes a powerful URL routing system for creating clean URLs, an ORM for working with databases, and an HTML template engine. It also has an authentication system, security features, and middleware support built in. Django has a vibrant community that is constantly improving and contributing to the framework. Because of its scalability, flexibility, and ease of use, it is a popular choice among developers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requests
&lt;/h3&gt;

&lt;p&gt;With the Python library Requests, HTTP requests are made easier. It was developed for usage with API requests and page scraping. Requests offer a straightforward and understandable interface for handling HTTP requests and responses. The package includes functions for managing cookies and sessions, authenticating users, and encoding data in several different forms. Moreover, Requests supports the HTTP/1.1 and HTTP/2 protocols and offers synchronous and asynchronous request handling. The programme is frequently used for tasks including automation, data extraction, and web development.&lt;/p&gt;

&lt;h3&gt;
  
  
  BeautifulSoup
&lt;/h3&gt;

&lt;p&gt;Beautiful Soup is a popular Python package for parsing HTML and XML documents. This package enables programmers to easily extract data from HTML pages. It makes it simple to use web scraping functions in Python. Beautiful Soup parses HTML documents and allows users to extract the content they require. Beautiful Soup's extensive documentation allows it to work with HTML files in a variety of formats. To efficiently extract content, the package supports a variety of searching and filtering methods. It is the go-to Python package for extracting data from web pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, the Python programming language is a very strong and flexible tool that can be applied to a wide range of projects, including web development and data analysis. The top 10 Python packages that every developer should be familiar with can help developers use Python to its best ability. These software packages offer strong tools and capabilities that can be utilised to create intricate applications and address intricate issues. These packages make it simple for developers to build dependable and effective apps. We trust that this article has helped you gain a better knowledge of the top 10 Python packages that every developer should be familiar with.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Deploying FastAPI application to Render</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Wed, 02 Nov 2022 06:09:37 +0000</pubDate>
      <link>https://dev.to/akashrchandran/deploying-fastapi-application-to-render-47i1</link>
      <guid>https://dev.to/akashrchandran/deploying-fastapi-application-to-render-47i1</guid>
      <description>&lt;p&gt;Recently, I came to know that Heroku is going to stop supporting free services. I have almost all the projects running in Heroku and I never tried any services. Many people pointed out that &lt;a href="https://render.com/"&gt;Render&lt;/a&gt; is the best free alternative to the Heroku. So I am giving it a try by hosting a FastAPI application. Render seems to directly support python frameworks like Flask, Django etc as their documentation mentions them. But we should be able to host FastAPI app as it supports building any python app, we just need to change the starting command. Let's get into it without wasting another minute.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating &lt;a href="https://render.com/"&gt;Render&lt;/a&gt; Account
&lt;/h2&gt;

&lt;p&gt;To host on Render, you'll need to sign up or create an account. It's free and supports direct login using google, GitHub and GitLab account. Or you can use email and password to signup. You need to verify your email to get full access to the platform. Just head to &lt;a href="https://dashboard.render.com/register"&gt;render/register&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jYd1-DcK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667276301389/gt22ZUVp5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jYd1-DcK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667276301389/gt22ZUVp5.png" alt="image.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Need for GitHub or GitLab account
&lt;/h2&gt;

&lt;p&gt;It's easier to deploy an application that has its source code hosted on GitHub or GitLab. Render connects with GitHub or GitLab to deploy your apps and websites automatically on every push to your project. This is very useful, as you don't have to manually deploy every time you make a change. You can either connect &lt;a href="https://github.com/signup"&gt;GitHub&lt;/a&gt; or &lt;a href="https://gitlab.com/users/sign_up"&gt;GitLab&lt;/a&gt; or both. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MtNZslMo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667288724821/nJSpN9UyJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MtNZslMo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667288724821/nJSpN9UyJ.png" alt="image.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's make a simple API using FastAPI
&lt;/h2&gt;

&lt;p&gt;We need an application to deploy to the Render. Let's build a simple API app which echos &lt;code&gt;Hello World&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up environment
&lt;/h3&gt;

&lt;p&gt;You'll need to download and install &lt;a href="https://www.python.org/"&gt;Python3&lt;/a&gt; to your local machine. It's better to set up a virtual environment for the application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Windows
&lt;/h4&gt;

&lt;p&gt;For windows, just head to &lt;a href="https://www.python.org/downloads/"&gt;python.org&lt;/a&gt; and download the latest EXE file and install. Installation is pretty well interactive, you just need to follow the instructions shown.&lt;/p&gt;

&lt;p&gt;Next, create a folder in your preferred directory and name &lt;code&gt;simple-api&lt;/code&gt;. Next to set up virtual environment enter the command opening terminal or CMD.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;py &lt;span class="nt"&gt;-3&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; venv .venv
.venv&lt;span class="se"&gt;\s&lt;/span&gt;cripts&lt;span class="se"&gt;\a&lt;/span&gt;ctivate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Linux or Mac OS
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Most of the Linux Distributions come with python3 be installed by default, just check if it exists by using command&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For mac users just head to &lt;a href="https://www.python.org/downloads"&gt;python.org&lt;/a&gt; and download the .pkg file and start installing.&lt;/p&gt;

&lt;p&gt;Next to set up virtual environment, on Linux sometimes we need to install extra library. To do the same, just enter the command in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;python3-venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, to initialize and activate a virtual environment on Mac and Linux, use the commands below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv .venv
&lt;span class="nb"&gt;source&lt;/span&gt; .venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installing requirements
&lt;/h2&gt;

&lt;p&gt;Make sure your package manager that is &lt;code&gt;pip&lt;/code&gt; is up-to-date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--upgrade&lt;/span&gt; pip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now install FastAPI, using &lt;code&gt;pip&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;FastAPI also requires an ASGI server, for production, such as Uvicorn or Hypercorn. I personally just use Uvicorn.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="s2"&gt;"uvicorn[standard]"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Coding the API
&lt;/h2&gt;

&lt;p&gt;Open the folder in any text editors, you can use notepad or advanced IDE's like VS Code or Sublime Text 4. Make a new file called &lt;code&gt;main.py&lt;/code&gt; and we use this file to write the code in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sN4rH5cj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667297849372/ABQUYVOTQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sN4rH5cj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667297849372/ABQUYVOTQ.png" alt="image.png" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just copy and paste the below lines to the file. If you're good with python or professional, then you can add or modify, making the API better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_root&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="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file. Let's try out the simple-api using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uvicorn main:app &lt;span class="nt"&gt;--reload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--74Gh5PaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667298287004/x2tXl1-jA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--74Gh5PaZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667298287004/x2tXl1-jA.png" alt="image.png" width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Head to &lt;code&gt;localhost:8000&lt;/code&gt; or &lt;code&gt;http://127.0.0.1:8000&lt;/code&gt;. You should see the &lt;code&gt;Hello World&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1VLXvj9x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667298527955/vWaggp7uH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1VLXvj9x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667298527955/vWaggp7uH.png" alt="image.png" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All done. Now we just need to create some extra files which are need by Render to identify it as a python application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Requirements.txt
&lt;/h2&gt;

&lt;p&gt;Usually I use external library &lt;a href="https://pypi.org/project/pipreqs/"&gt;pipreqs&lt;/a&gt;, but you can just simply type  &lt;code&gt;fastapi&lt;/code&gt; and &lt;code&gt;uvicorn&lt;/code&gt; in the requirements.txt file. When deploying, render will install the latest version of these modules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_FRrokPD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667299816413/yHl-oQCm4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_FRrokPD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667299816413/yHl-oQCm4.png" alt="image.png" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a .gitignore file
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.gitignore&lt;/code&gt; file helps to avoid uploading of unwanted or private files to the git clients such as GitHub or GitLab. When sharing your code with others, there are often files or parts of your project, you do not want to share. So these files or folders can be mentioned in the .gitignore file.&lt;/p&gt;

&lt;p&gt;For this simple-api we don't want to upload &lt;code&gt;.venv&lt;/code&gt; and &lt;code&gt;__pycache__&lt;/code&gt; to the GitHub. So we mention them in the .gitignore file like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fv23AR_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667300636574/GVe_fPtPG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fv23AR_c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667300636574/GVe_fPtPG.png" alt="image.png" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Upload to GitHub or GitLab
&lt;/h2&gt;

&lt;p&gt;I am using the GitHub here, but you can use GitLab as well. I have installed &lt;a href="https://git-scm.com/download/"&gt;git&lt;/a&gt;, and I have connected it to my GitHub account. You'll find many articles on how to do it, just google it. If you're using Visual Studio then it has in built GitHub plugin which makes it way easier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WTFNLxL8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667300190867/CSsAfm85e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WTFNLxL8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667300190867/CSsAfm85e.png" alt="image.png" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have successfully uploaded to GitHub, you can find mine at &lt;a href="https://github.com/akashrchandran/simple-api"&gt;akashrchandran/simple-api&lt;/a&gt;. You should definitely add a readme.md to the repo, I will do it later.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zAg0tkuk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667300972758/ehibPeKFM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zAg0tkuk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667300972758/ehibPeKFM.png" alt="image.png" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying to Render.com
&lt;/h2&gt;

&lt;p&gt;As we are not deploying a static site, we should choose the option &lt;code&gt;Web Service&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XuI6A9no--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667302637873/HPSesfyBd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XuI6A9no--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667302637873/HPSesfyBd.png" alt="firefox_6Ko2SbNN6r.png" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have connected your GitHub account, then you can just search the repository name and find it there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xZuBA9Jh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667302811886/LFMSurQ_s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xZuBA9Jh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667302811886/LFMSurQ_s.png" alt="image.png" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give a name to your web-service, then to select the region which is nearest to you, for me is Singapore. Leave all other values default. For the Start command, copy and paste the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uvicorn main:app &lt;span class="nt"&gt;--host&lt;/span&gt; 0.0.0.0 &lt;span class="nt"&gt;--port&lt;/span&gt; 10000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HKvP6pfr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667303284903/-M7LNMQEs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HKvP6pfr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667303284903/-M7LNMQEs.png" alt="image.png" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Deploying may take some time, so patiently wait. After successful deployment, you should see the message like this below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wMfEw8sp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667303740868/ZbGt5Czak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wMfEw8sp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1667303740868/ZbGt5Czak.png" alt="image.png" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations, you have successfully deployed your FastAPI application to the render platform. You can view the site I deployed &lt;a href="https://simple-api-hr3q.onrender.com/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;It was so simple to deploy a FastAPI based application to render. If you don't want to code or waste time on making a FastAPI based app, then you can try deploying mine. It is available on &lt;a href="https://github.com/akashrchandran/simple-api"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="//render.com"&gt;Render&lt;/a&gt; may the Heroku alternative thing you're looking for, as it gives out almost the same free specifications as Heroku free tier. Do give it a try!&lt;/p&gt;

&lt;p&gt;Thanks for reading...&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Simple movies Telegram bot using PHP</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Wed, 26 Oct 2022 00:24:32 +0000</pubDate>
      <link>https://dev.to/akashrchandran/simple-movies-telegram-bot-using-php-4jf7</link>
      <guid>https://dev.to/akashrchandran/simple-movies-telegram-bot-using-php-4jf7</guid>
      <description>&lt;p&gt;I have been using telegram for almost 4+ years and have made more than 30 bots during this period. I made the first one in PHP and the rest were build in Python and JavaScript. So today I thought why not make a simple telegram bot in PHP. That's why we are going to make a movie's bot in PHP. Let's begin without wasting time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gathering Requirements
&lt;/h2&gt;

&lt;p&gt;Before we start making our bot, we need two things. It won't take much time, only few minutes. The things we need are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Telegram Bot Token&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;TMDB API key&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TMDB is the site from which we will pull the movie information from. It's free, you just need to register and fill some forms for an API key. So let's start gathering them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get Telegram Bot token
&lt;/h3&gt;

&lt;p&gt;It's actually pretty simple to get the token from telegram. But first you need is a telegram account. I assume that you have one. Search for a bot named BotFather or use this &lt;a href="https://tx.me/BotFather"&gt;link&lt;/a&gt;. It should have a verified emoji in its name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GD4l0_2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1665903526384/b8KOpi0si.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GD4l0_2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1665903526384/b8KOpi0si.png" alt="image.png" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click on the bot and press start, or if you don't see that option, then type &lt;code&gt;/start&lt;/code&gt;. I should greet you with a help message. You can read through that if you want to, it lists all details about what the bot can do. Next is, type &lt;code&gt;/newbot&lt;/code&gt; and press send. It will ask for the name of the bot. After that it will ask for bot username which must be unique and should end in bot like mine here is &lt;code&gt;phpmoviesbot&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LoojPan8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1665906428085/hpped5n9A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LoojPan8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1665906428085/hpped5n9A.png" alt="4HOD9NPmBo.png" width="800" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you will receive a message which has a http API token. This was the token we were looking for, save it somewhere because we will need it later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get API key from TMDB
&lt;/h3&gt;

&lt;p&gt;Head to &lt;a href="https://www.themoviedb.org/"&gt;TMDB&lt;/a&gt; and find register or join now. Then create an account. Then find settings and on the left side you will see an option named API, press that or use this &lt;a href="https://www.themoviedb.org/settings/api"&gt;link&lt;/a&gt;. You will need to fill a form about what you will be using the API for etc, just fill it yourself. After that you will get an API Key (v3 auth), save it somewhere we need it later.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1AfnlakS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1665907514115/DQWWgGzYK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1AfnlakS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1665907514115/DQWWgGzYK.png" alt="ELYBOncHjY.png" width="800" height="451"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have what we wanted, let's start with the programming side.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up webhook
&lt;/h2&gt;

&lt;p&gt;Setting up the webhook through which your Telegram bot will communicate is the next step in the creation process. In order to avoid having to query the API every few minutes (or seconds) to see whether, for example, a new message has been received, APIs use webhooks to notify you that something has occurred.&lt;/p&gt;

&lt;p&gt;Telegram only makes use of one form of webhook, which delivers an &lt;code&gt;update&lt;/code&gt; object each time something occurs. The webhook configuration is really simple.&lt;br&gt;
There are only two things you must be aware of: your API token (which you should already have from step one), and the URL at which your bot will be hosted.&lt;br&gt;
It will have a URL similar as &lt;code&gt;https://yourdomain.com/yourbot.php&lt;/code&gt;.&lt;br&gt;
To ensure that Telegram sends the webhook, insert &lt;code&gt;https&lt;/code&gt; at the beginning of the URL.&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;https://api.telegram.org/&amp;lt;TELEGRAM_BOT_TOKEN&amp;gt;/setwebhook?url=https://yourdomain.com/yourbot.php&lt;/code&gt; in a standard web browser now. Do replace &lt;code&gt;&amp;lt;TELEGRAM_BOT_TOKEN&amp;gt;&lt;/code&gt; with your telegram bot token.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mgbgpXiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1666591475038/-VkfPORHR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mgbgpXiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1666591475038/-VkfPORHR.png" alt="image.png" width="528" height="322"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Coding part
&lt;/h2&gt;

&lt;p&gt;For this, I am assuming you know basic PHP syntax, formattings etc. Let's start then, we know that PHP script start with &lt;code&gt;&amp;lt;?php&lt;/code&gt; and ends with &lt;code&gt;?&amp;gt;&lt;/code&gt; and we write the code in between.&lt;br&gt;
We are receiving webhook, therefore let's get input from the POST body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'php://input'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$update&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The received data is in JSON, to decode it to PHP based array, we have to use &lt;code&gt;json_decode&lt;/code&gt; function. Now let's declare some variables which will store some, data from the update like username, chat ID etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$update&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'chat'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$update&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'text'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$update&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'from'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We would have to send many messages, so let's define it directly as a function to make it easier to reuse.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;send_message&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$apiToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;TELEGRAM_BOT_TOKEN&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;urlencode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.telegram.org/bot&lt;/span&gt;&lt;span class="nv"&gt;$apiToken&lt;/span&gt;&lt;span class="s2"&gt;/sendMessage?chat_id=&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;text=&lt;/span&gt;&lt;span class="nv"&gt;$text&lt;/span&gt;&lt;span class="s2"&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;Replace the &lt;code&gt;&amp;lt;TELEGRAM_BOT_TOKEN&amp;gt;&lt;/code&gt; with the telegram bot token you made. Let's test the bot with a &lt;code&gt;/start&lt;/code&gt; message, to do that we have to write some more code, just an if statement as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'/start'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;send_message&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Hey @&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;send me any query to search for the movie."&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;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h1MGGEaw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1666584937091/88jUlYr05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h1MGGEaw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1666584937091/88jUlYr05.png" alt="image.png" width="800" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It seems to be working. Now let's complete it by adding the movie information fetching part. I want to make the bot search everything we text we send except &lt;code&gt;/start&lt;/code&gt;. So we just add else to the pre-existing if statement. I am expecting you know how to parse JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.themoviedb.org/3/search/movie?api_key=&amp;lt;TMDB_API_KEY&amp;gt;&amp;amp;language=en-US&amp;amp;page=1&amp;amp;query=&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="s1"&gt;'results'&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="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&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="nv"&gt;$title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$overview&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'overview'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$popularity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'popularity'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$release_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'release_date'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$vote_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'vote_count'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nf"&gt;send_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"
        Title: &lt;/span&gt;&lt;span class="nv"&gt;$title&lt;/span&gt;&lt;span class="s2"&gt;
        Description: &lt;/span&gt;&lt;span class="nv"&gt;$overview&lt;/span&gt;&lt;span class="s2"&gt;
        Popularity : &lt;/span&gt;&lt;span class="nv"&gt;$popularity&lt;/span&gt;&lt;span class="s2"&gt;
        Release Date : &lt;/span&gt;&lt;span class="nv"&gt;$release_date&lt;/span&gt;&lt;span class="s2"&gt;
        Votes: &lt;/span&gt;&lt;span class="nv"&gt;$vote_count&lt;/span&gt;&lt;span class="s2"&gt;"&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="nf"&gt;send_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Couldn't find any movie with that name!"&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;Replace &lt;code&gt;&amp;lt;TMDB_API_KEY&amp;gt;&lt;/code&gt; with the TMDB API KEY we gathered. Now let's try the bot, by sending a movie name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--s6MTeXGP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1666590852520/1b-UXeM8D.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--s6MTeXGP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1666590852520/1b-UXeM8D.png" alt="image.png" width="800" height="559"&gt;&lt;/a&gt;&lt;br&gt;
Works perfectly, You can add more functions to it, but for now I am stopping it here.&lt;/p&gt;
&lt;h2&gt;
  
  
  Summed up
&lt;/h2&gt;

&lt;p&gt;We learnt to create a small bot using webhooks. I will add an article about host the bot, for free, on vercel. The entire code for the bot is available below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="nv"&gt;$content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file_get_contents&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'php://input'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$update&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$chat_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$update&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'chat'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$update&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'text'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$update&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'from'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'username'&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="nv"&gt;$message&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'/start'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;send_message&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Hey @&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="s2"&gt;  &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;send me any query to search for the movie."&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="nv"&gt;$req&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.themoviedb.org/3/search/movie?api_key=&amp;lt;TMDB_API_KEY&amp;gt;&amp;amp;language=en-US&amp;amp;page=1&amp;amp;query=&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nv"&gt;$req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="s1"&gt;'results'&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="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$movie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&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="nv"&gt;$title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$overview&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'overview'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$popularity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'popularity'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$release_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'release_date'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nv"&gt;$vote_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$movie&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'vote_count'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nf"&gt;send_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"
        Title: &lt;/span&gt;&lt;span class="nv"&gt;$title&lt;/span&gt;&lt;span class="s2"&gt;
        Description: &lt;/span&gt;&lt;span class="nv"&gt;$overview&lt;/span&gt;&lt;span class="s2"&gt;
        Popularity : &lt;/span&gt;&lt;span class="nv"&gt;$popularity&lt;/span&gt;&lt;span class="s2"&gt;
        Release Date : &lt;/span&gt;&lt;span class="nv"&gt;$release_date&lt;/span&gt;&lt;span class="s2"&gt;
        Votes: &lt;/span&gt;&lt;span class="nv"&gt;$vote_count&lt;/span&gt;&lt;span class="s2"&gt;"&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="nf"&gt;send_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Couldn't find any movie with that name!"&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="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;send_message&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$apiToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;TELEGRAM_BOT_TOKEN&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;urlencode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nb"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.telegram.org/bot&lt;/span&gt;&lt;span class="nv"&gt;$apiToken&lt;/span&gt;&lt;span class="s2"&gt;/sendMessage?chat_id=&lt;/span&gt;&lt;span class="nv"&gt;$chat_id&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;text=&lt;/span&gt;&lt;span class="nv"&gt;$text&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>telegram</category>
      <category>programming</category>
      <category>php</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Some less popular git commands</title>
      <dc:creator>Akash R Chandran</dc:creator>
      <pubDate>Sun, 16 Oct 2022 02:38:48 +0000</pubDate>
      <link>https://dev.to/akashrchandran/some-less-popular-git-commands-2oem</link>
      <guid>https://dev.to/akashrchandran/some-less-popular-git-commands-2oem</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Git is pretty much used by every developer nowadays. Personally, even after using it for approximately two years, I continue to discover new git features. So here are some of the commands I discovered, and seems they aren't popular enough. Without wasting time, let's dive into it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check logs but in easier ways
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git log&lt;/code&gt; a useful tool for looking back and reading the history of every action taken with a repository. A git log's multiple settings can be utilized to narrow down history. The git log typically contains a list of commits. And sometimes the list is so big it's hard to find anything we need. Let me show you an easier way to do the same, but the output is short yet brief.&lt;/p&gt;

&lt;h3&gt;
  
  
  git log --oneline
&lt;/h3&gt;

&lt;p&gt;It's super helpful to check the latest commits. With the &lt;code&gt;--oneline&lt;/code&gt; flag, each commit is reduced to a single line. It just shows the commit ID and the first line of the commit message by default. The output should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1266f06 (origin/develop) updated readme
33bf5fb Merge branch 'develop'
bd99bba fixed error
19389a7 Merge pull request #5 from akashrchandran/develop
5a2258e added exceptions
9dbb9de updated readme
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  git shortlog
&lt;/h3&gt;

&lt;p&gt;A modified version of the git log command called &lt;code&gt;git shortlog&lt;/code&gt; is used mostly to make release notifications. It displays the first line of each commit message and groups each commit according to author. This makes it simple to determine who has been working on what.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Akash R Chandran (4):
      Initial commit
      fixed all errors
      added gitignore
      Added support for heroku deployment

Paulo Martini(3):
      added files via upload
      updated
      fixed requests errors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  git log --graph
&lt;/h3&gt;

&lt;p&gt;The branch structure of the commit history is represented via an ASCII graph when the &lt;code&gt;--graph&lt;/code&gt; option is used. To make it simpler to determine which commit belongs to which branch, this is frequently used in conjunction with the &lt;code&gt;--oneline&lt;/code&gt;  and &lt;code&gt;--decorate&lt;/code&gt; commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*   64f95a3 (HEAD -&amp;gt; main, origin/main, origin/HEAD) Merge pull request #6 from akashrchandran/develop
|\
| * 1266f06 (origin/develop) updated readme
* | 33bf5fb Merge branch 'develop'
|\|
| * bd99bba fixed error
* | 19389a7 Merge pull request #5 from akashrchandran/develop
|\|
| * 5a2258e added exceptions
|/
* 9dbb9de updated readme
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get more info on &lt;code&gt;git log&lt;/code&gt; you can visit &lt;a href="https://www.atlassian.com/git/tutorials/git-log"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switching branches
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;git checkout&lt;/code&gt; command can be used for a wide range of purposes. Because of this, the Git community has decided to release a new command: &lt;code&gt;git switch&lt;/code&gt;. It was designed primarily for the process of switching branches, as the name suggests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git switch develop
Switched to a new branch &lt;span class="s1"&gt;'develop'&lt;/span&gt;
branch &lt;span class="s1"&gt;'develop'&lt;/span&gt; &lt;span class="nb"&gt;set &lt;/span&gt;up to track &lt;span class="s1"&gt;'origin/develop'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Switch back and forth between two branches
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;git switch&lt;/code&gt; makes it very easy to switch between the same branches back to back. It doesn't even need the branch name to switch, just remove branch name and put a hyphen&lt;code&gt;(-)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git switch -
Switched to branch &lt;span class="s1"&gt;'main'&lt;/span&gt;
Your branch is up to &lt;span class="nb"&gt;date &lt;/span&gt;with &lt;span class="s1"&gt;'origin/main'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the same command again will switch back to &lt;code&gt;develop&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking the changes made to file
&lt;/h2&gt;

&lt;p&gt;The function of diffing outputs the differences between two input data sets. A diff operation is performed on Git data sources when the Git command &lt;code&gt;git diff&lt;/code&gt; is invoked. Commits, branches, files, and other types of data sources are examples. This document will go over typical &lt;code&gt;git diff&lt;/code&gt; commands and diffing work flow patterns. &lt;code&gt;git diff&lt;/code&gt;, &lt;code&gt;git status&lt;/code&gt;, and &lt;code&gt;git log&lt;/code&gt; are frequently used to examine the present state of a Git repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git diff 
diff &lt;span class="nt"&gt;--git&lt;/span&gt; a/spotify.php b/spotify.php
index 6987fef..bc766cf 100644
&lt;span class="nt"&gt;---&lt;/span&gt; a/spotify.php
+++ b/spotify.php
@@ &lt;span class="nt"&gt;-64&lt;/span&gt;,6 +64,7 @@ class Spotify
                curl_setopt&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$ch&lt;/span&gt;, CURLOPT_RETURNTRANSFER, TRUE&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                curl_setopt&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$ch&lt;/span&gt;, CURLOPT_URL, &lt;span class="nv"&gt;$formated_url&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; curl_exec&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$ch&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
+               &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Restore to an older version of the file
&lt;/h2&gt;

&lt;p&gt;Another extremely useful option available with the git restore command is "—source". You can quickly restore any earlier version of a particular file with the use of this option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git restore &lt;span class="nt"&gt;--source&lt;/span&gt; 6bcf266b index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding comments to your commits
&lt;/h2&gt;

&lt;p&gt;To preserve a better development history, developers typically write clear and self-explanatory commit notes. Thanks to well-liked commit conventions, some development teams even automatically produce release notes with commit messages. How can I include some extra notes in a commit? Commit message additions will be reflected on remote Git hosting GUIs. How can I record a message for a CI/CD server or add a hidden note to remember about any technical work? We can add extra comments for commits with the help of the built-in Git notes functionality.&lt;/p&gt;

&lt;p&gt;To connect a note to the current commit, issue the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git notes add &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"This is a test comment"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By omitting the -m option, you can use the editor programme to write a multiline, lengthy note. The git notes show command allows you to view the most recent note. Additionally, the git log command by default displays notes.&lt;br&gt;
Git notes were previously supported by GitHub as comments, but they are now deprecated in favour of web-based comments.&lt;/p&gt;

&lt;p&gt;Git does not automatically push or pull notes, but you can manage remote notes explicitly using the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &amp;lt;remote&amp;gt; refs/notes/&lt;span class="k"&gt;*&lt;/span&gt;
git fetch origin refs/notes/&lt;span class="k"&gt;*&lt;/span&gt;:refs/notes/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Print the SHA1 hashes
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git rev-parse&lt;/code&gt; is an ancillary plumbing command primarily used for manipulation.&lt;br&gt;
One common usage of &lt;code&gt;git rev-parse&lt;/code&gt; is to print the SHA1 hashes given a revision specifier. In addition, it has various options to format this output, such as --short for printing a shorter unique SHA1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git rev-parse HEAD
64f95a3edf8ecaf1f874b9a25fb7312a722f463a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These commands are mostly for better productivity and simplicity. It's good if you know what they do. Git is well known these days, but many of its powers are still unknown to the public. It's true that you can "survive" with a handful of commands like commit, push, and pull.&lt;/p&gt;

&lt;p&gt;Thanks for reading 😊.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>githunt</category>
      <category>gitcommands</category>
    </item>
  </channel>
</rss>
