In this video, we are going to be taking steps
to ensure that our backend project is ready
for us to start developing our endpoints.
Let's begin.
First, we need to head into the root of our
backend folder.
Here we need to create a virtual environment.
To do that, we need to open up our terminal.
Let's go to backend.
And here we can type the command to create
our virtual environment.
Once the environment is created, as you can
see here we have a new folder, the .vmv
folder
that we specified in our command.
We can now activate this environment.
For that, you can use the following command
on macOS or Linux.
say source.vmv
bain activate.
And now, as indicated by this prefix to our
command prompt,
we are now in our virtual environment.
So we can clear all this.
This activation command is slightly different
on Windows.
You don't need to begin with source.
And you simply replace the bain folder with
a scripts folder.
And just as I said earlier, once this virtual
environment is
activated, you can see it as the prefix to
your command prompt.
Next we'll need to install our dependencies.
These are the dependencies that are listed
in our requirements.txt
file.
We are going to be installing all of them
with one single command.
So let's head over to our command prompt and
at the root of our backend folder, we'll
We'll say pip install –r then we target our
requirements.txt
file.
This will begin the installation and in no
time, we should have all listed packages installed.
Awesome.
Now, our installation is done.
Let's just click out of our terminal for now.
The next thing we have to do is to have our
Gemini API key in place and accessible to
our backend application.
API key is a very sensitive part of your application
and you should always make sure it is securely
stored and accessed.
This is why we installed the python.env
package in order to store and access our API
key from
an environment variable.
So let's create a .env
file at the root of our backend folder.
Inside this folder, let us add the environment
variable GeminiAPIKey.
We'll then set the value of this variable
to the API key we just created in Google's
AI Studio.
Perfect.
Now save this file and make sure that it is
ignored in the .gitignore
file.
We have already ignored it in the .gitignore
file that comes with this seed project.
So you're good on that front, but in your
other projects, always make sure that you
never push this .env
file to a remote repository.
Our next task is to create the entry point
Python file to this backend application.
We are going to be setting up this file so
that in subsequent videos, we can start writing
our endpoints right away.
So let's create the file main.py
at the root of the backend folder.
Now inside this file, first, let us take care
of our imports.
We'll be starting with our FastAPI dependencies.
First, let's say from FastAPI, import FastAPI
file, upload file, and HTTP exception.
We'll also be adding some middleware for calls
so I'm going to say from first api.middleware.calls
From there, we import the calls middleware
model.
I think we can put this aside for now.
Next, let us import the dependencies for working
with our Gemini API.
to say from Google, import genai, and from
Google.genai,
import types.
Here we have imported our Gemini Python SDK,
and also
some types on the API that we're going
to be using along the course of building this
project.
Finally, let us get in some helpful system
dependencies
for loading our environment variables
working with files.
Come down here and say from .env,
let us import the
load.env
function and also import the OS package.
And while we're on that, let us
call our load.env
method to load all our environment variables
from our .env
file.
Go down here and say load.env
and call it.
Perfect.
The next thing we're
going to do is to get a reference to our Gemini
API key so that we can use it in
our Gemini API requests.
So let's do that.
I'm going to go down a little bit,
Here, I'm going to say Gemini API key and
let us get it from the environment.
After that, let us check if we have it.
So if not Gemini API key, let us simply raise
a value error.
And here, we'll say Gemini API key not found
in the .env
file.
Let us also define the Gemini model we'll
be using.
We're setting that using a constant.
And for this course, we're going to be using
Gemini 2.5
Flash, a powerful, fast, multimodal model.
And we'll wrap up our Gemini setup section
by creating a new instance of our Gemini API
client.
Awesome.
The next setup we're going to be doing
is for our FastAPI server.
So let us go ahead and start that
by first creating a new instance of the FastAPI
server.
We say app equals FastAPI.
simply passing a title for our application.
We're going to be calling that the Travel
Companion AVI.
Next, let us enable some call settings for
smooth communication with the front-end.
down here by adding the middleware, say app.addMiddleware,
we're passing causeMiddleware, and then impute
our settings to allow the client to communicate
with our server without being blocked by any
calls rules.
Need to put some quotes around that.
Say allowCredentials.
Set that to true.
is allowMethods, we want to allowAllMethods,
and finally, allowHeaders, and we also want
to allowAllHeaders.
Good.
Now, this is for a demo application in production.
You want to make sure that you have stricter
rules for calls.
But for this application, this is simply great.
Now we can start writing our endpoints.
So we'll be wrapping up this video by creating
a simple base endpoint and running our backend
application to see that it's good to go.
So let's write a simple root endpoint that
simply spits out a generic message.
We'll just scroll down and here we'll say
app.get,
define our root endpoint and define
the handler that is going to undo that.
Say with root and here we can just simply
return a message wrapped in a dictionary and
And this will say Travel Companion API is
running.
Good.
Now we have our route endpoint.
But to run this application, we need to write
the code that allows us to run our server
through this file.
So let us do that.
We're going to be making use of UVicon.
But first, let us make sure that we are checking
that this file is what we are running directly
from our terminal, say ifname="main", then
we simply import uvconn and we can use uvconn
to run this app and set the host to 0.0.0.0
and our port to 8000.
Awesome.
Now, with this in place, we can go to our
terminal and run our server.
So let us pull up the terminal.
Make sure that you are still at the root of
the backend folder.
And here we can simply run python main.py
hit enter.
And now that our server is running, let's
simply visit our base URL in the browser.
So here in the browser, we can simply say
localhost.
Our server is running at the port 8000.
So we say colon 8000 and hit enter.
And here you can see the message travel companion
API is running.
How smooth is that?
I think we should also have a docs endpoint.
So we say slash docs.
And here as you can see, we have our root
endpoint.
Awesome.
So we have our server running and we now have
everything that we need to start building
our application.