As a newbie, API has always been mystical to me. I have read multiple definitions and watched multiple Youtube videos, but it was still abstract.

The common explanation is always something like: “a software intermediary that allows two applications to talk to each other.” or “we can send a request to it to receive the information that…” and etc…

but still, is it a physical thing? a package that I can import and use? or something similar to a programming language? I know, it allows applications to talk to each other, but, WHAT IS THAT THING?

If you have the same question in your mind, read on. I hope this blog might help a bit.

API

API is a block of codes. Here, you are looking at one now.

#Part 1: URL & method
	  @app.route('/questions', methods=['GET'])
	  def get_questions():
	    # Part 2: get all questions from database and paginate
	    selection = Question.query.all()
	    total_questions = len(selection)
	    current_questions = paginate_questions(request, selection)

	    # Part 2: get all categories from database
	    categories = Category.query.all()
	    categories_dict = {}
	    for category in categories:
	        categories_dict[category.id] = category.type

	    # abort 404 if no questions
	    if (len(current_questions) == 0):
	        abort(404)

	    # Part 3: return data to view
	    return jsonify({
	        'success': True,
	        'questions': current_questions,
	        'total_questions': total_questions,
	        'categories': categories_dict
	    })

This is one of my APIs for my most recent project. The project is a web application that allows people to play a trivia game. This API allows the web app to display all the questions and the questions can be displayed by categories as well.

What makes this block of codes an API?

In my perspective, it can generally be divided into 2 big parts.

The first part is Part 1: URL and Methods. This part serves 2 purposes. Firstly, the**_ @_**_app.route(…)_ is the noticeable signal that tells you, you are looking at an API. Secondary, in this part, the API is listening to the request from the client, namely the URL address that was passed in.

If you want to know more about why the _@app.route()_ works and why every API starts this way, here are two good resources that will help.

Flask: why app.route() decorator, should always be the outermost?

Things which aren’t magic - Flask and @app.route - Part 1

Part 2 and Part 3 are the essences or the jobs of API. They are why we need an API. In Part 2, API retrieves the information that was requested by the client from the database. In Part 3, API tells the server how it should respond to the client so that the client understands.

If the above paragraphs are abstract. Look at the codes below. These were my frontend code. Look how they correspond to each other. Just a footnote, I used React and Flask and for this app.

  getQuestions = () => {
	    $.ajax({
	      url: `/questions?page=${this.state.page}`,        //URL Match
	      type: "GET",                                     //Method Match
	      success: (result) => {
	        this.setState({
	          questions: result.questions,                //result name match
	          totalQuestions: result.total_questions,     //result name match
	          categories: result.categories,              //result name match
	          currentCategory: result.current_category }) //result name match
	        return;
	      },
	      error: (error) => {
	        alert('Unable to load questions. Please try your request again')
	        return;
	      }
	    })
	  }

#api #software-development #programmer #computer-networking #technology

Reflection on My First API Project
1.55 GEEK