Intro to APIs

What are APIs?

The best example I can give for a use case for an API is for a simple request to a database:

{
"error": "Invalid Credentials"
}
{
"username": "matted",
"email": "matted@cs.house",
"phone": 5555555555
}

What is RESTful?

  • GET — retrieve a specific resource (by id) or a collection of resources
  • POST — update a specific resource (by id)
  • PUT — create a new resource
  • DELETE — remove a specific resource by id

Paths

Root

Routes

Parameters

  • ei: 87CEWsH-L8qV_Qau8ILABw
  • q: api
  • gs_l: psy-ab.3..35i39k1l2j0i131k1j0j0i20i264k1j0i131k1j0i20i264k1j0i131k1j0l2.3075.3336.0.4076.3.3.0.0.0.0.81.215.3.3.0….0…1c.1.64.psy-ab..1.3.215…0i131i20i264k1.0._czjqWn7yUE

Status Codes

  • 1xx: Informational responses.
  • 2xx: Success.
  • 3xx: Redirection.
  • 4xx: Client errors.
  • 5xx: Server errors.

Make your Own

git clone https://github.com/devinmatte/Your-First-API.git
git checkout [branch]
  • Python with Flask: python_flask
  • Java with Spring: java_spring
  • PHP with Slim: php_slim
  • Node with Express node_express

Tools

Getting started

Python/Flask

  • First run pip install -r requirements.txt
  • Make sure you’re using Python3, you may need to use pip3 instead of pip on some systems.
  • Edit __init__.py and add the following route, then run app.py to test.
@app.route("/", methods=["GET"])
def root():
return "Hello World!"

Java/Spring

  • I recommend using an IDE for all Java Development
  • I also recommend using Intellij
  • Add build.gradle and pom.xml to the gradle/mavenportions of your IDE
  • Make sure to mark src as the sources directory
  • Run Application.java to run/test your application
@RequestMapping("/") 
public String root() {
return "Hello World!";
}

PHP/Slim

  • First run composer install
  • If you don’t have composer, install it
  • Edit routes.php and add the following route, then run php -S localhost:3000 -t index.php to test.
$app->get('/', function (Request $request, Response $response) {
return $response->write("Hello World!");
});

Node/Express

  • First run npm install
  • If you don’t have node, install it
  • Edit app.js and add the following route, then run node app.js to test.
app.get('/', (req, res) => res.send('Hello World!'));

Multiple Request Types

Python/Flask

Java/Spring

PHP/Slim

Node/Express

Using Status Codes

Python/Flask

# Return a 205 
@app.route("/success", methods=["GET"])
def return_success():
return "This will return a 205 Status Code", 205
# Return a 404
@app.route("/fail", methods=["GET"])
def return_fail():
return "This will return a 404 Status Code", 404

Java/Spring

// Return a 205 
@RequestMapping(value = "/success", method = RequestMethod.GET)
public ResponseEntity returnSuccess() {
return ResponseEntity.status(205).body("This will return a 205 Status Code");
}
// Return a 404
@RequestMapping(value = "/fail", method = RequestMethod.GET)
public ResponseEntity returnFail() {
return ResponseEntity.status(404).body("This will return a 404 Status Code");
}

PHP/Slim

// Return a 205 
$app->get('/success', function (Request $request, Response $response) {
return $response->write("This will return a 205 Status Code")->withStatus(205);
});
// Return a 404
$app->get('/fail', function (Request $request, Response $response) {
return $response->write("This will return a 404 Status Code")->withStatus(404);
});

Node/Express

// Return a 205 
app.get('/success', function (req, res) {
res.status(205);
res.send('This will return a 205 Status Code');
});
// Return a 404
app.get('/fail', function (req, res) {
res.status(404);
res.send('This will return a 404 Status Code');
});

Get Data to Return

Python/Flask

Java/Spring

PHP/Slim

Node/Express

Continue Learning

Frameworks

--

--

--

Software Engineering Student at Rochester Institute of Technology with a focus on Full Stack Web Development and DevOps

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Web App V 0.0.1 is LIVE

Adding Sound FX to the 2D Space Shooter

Proper handling of ‘make’ query terms in e-commerce search

5 Common Server Backup Issues

Automated Investing: How I Research More Stocks in Less Time Using Python

Liquid Web vs Nexcess: 2021 Review

5 Ways to Help Manage Anxiety When Learning to Code

Timing with cURL

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Devin Matté

Devin Matté

Software Engineering Student at Rochester Institute of Technology with a focus on Full Stack Web Development and DevOps

More from Medium

JavaScript Fundamentals in 5 minutes

Java Script , Version Control and NoSQL

5 Essential Java Skills That Can Help You With Rust Programming?

Avoid Using Console.log()