Today I Learned How to Make a Static API With Flask & Sqlite
Tags: devjournal, 100daysofcode, python, webdev
Published: 2021-10-21 05:57:29

This post was initially adapted from [here](https://programminghistorian.org/en/lessons/creating-apis-with-python-and-flask). I wrote this to make it simpler to understand, more like a straight forward approach to creating API's in Python using flask routes.

Link to [repository](https://github.com/munazhe/api-template)


&nbsp;
&nbsp;


### 1 ==> This is where our imports happen. All that we are going to need in our project.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cifzr1l52hm6hq8xggd1.png)


### 2 ==> We also want to make sure our Flask server is running in debug mode so that we can see when things go wrong.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ult2vva38sjhufzkxqez.png)

### 3 ==> Our iterator that keeps track of index and object we're looking for int the given database.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e11wl0f7p82gh58r512h.png)


### 4 ==> This is our welcome page, but feel free to add whatever custom message you want.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1a2ktgiv2fctc4aq8btv.png)

### 5 ==> The `/all` router that fetches all columns and row in the database without filter.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5yyopqt6a42rq7isbpkq.png)


### 6 ==> If we hit a dead-end in our database or if you can't find what you looking for. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/naw058i6zslm9wq51ytw.png)


### 7 ==> Does the opposite of the #5 except you can add more than one column_filter per request, unlike traditional static APIs.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8sbgjoa0q5ccr4qb19ns.png)

### 8 ==> If you can't run how'd your program be alive; I'm guessing it would be a zombie process? 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2myoabzl23z8t1r9arf6.png)




&nbsp;
### Here's how it all works: 
&nbsp;
&nbsp;


#### 1. Essentially, the variable `query` gets populated with however many column_filters (parameters) we want to use on our database. This is even better than rerouting every single column in the database onto a different url like in traditional static APIs. 
&nbsp;


#### 2. It's all about `query`
In the `/api/v1/resources/books?column_filter` we notice that inside the api_filter(), the variable `query` is declared as a string. However, right below follows a series of boolean expressions that make our filtering possible.
&nbsp;

Within these booleans, all the operations inside are pretty much the same. Our variable query gets populated by an additional string each step of the way; in the first place being we have: `query += ' id=? AND'`. 
&nbsp;

#### 3. cur.execute()
But finally at the end of the logical expressions; our results are assigned to `cur.execute()`, which holds our query and column_filter(s). Basically meaning when our JSON gets displayed, `to_filter.fetchall` is going to show us a display of all the filters inside our query statement. 
&nbsp;
> Just because you have the query, it don't mean I will show you the results. 

Even if the SQL database finds our results, it's still our repsonsibliity as the developer to `fetchall()` using a filter in this case, it's to_filter, populated as a result of the prior logical expression. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju27n9soodm81y28obb8.png)



&nbsp;
#### 4.  dict_factory()
dict_factory iterates our database while keeping track of our index and Object. Finally, the result is a an Object that then prepares us to turn the data into JSON format for us to interact with on the front-end. 


&nbsp;
&nbsp;
&nbsp;
&nbsp;
What would be really cool to see is how to write to our database coming from JSON format into our SQLITE database.