BILLY Talks

Part 02: API 101

API

... Application Programming Interface ...

An API is a software intermediary that allows two applications to talk to each other. In other words, an API is the "messenger" that delivers your request to the provider that you're requesting it from and then delivers the response back to you.

A Messenger ??

... A messenger of what? Sorrow? ...

No of course not.. It's all happy times with API's

Developers use API's to send a request and get a response back

This request/response pattern is what makes up the messages

Requests are done over either the HTTP or HTTPS protocol

Responses are typically either returned in JSON or XML

Here's an example of how a weather API might work

Types of API's

  • REST
  • SOAP


There are a few others, but don't really concern us
so F'em!!

REST

... Representational State Transfer ...

REST is not a protocol like the other web services, instead, it is a set of architectural principles. The REST service needs to have certain characteristics, including simple interfaces, which are resources identified easily within the request and manipulation of resources using the interface.

REST Methods

Communication over the HTTP or HTTPS protocol using the following HTTP spec methods allows for an almost plain english type of communication

These methods are idempotent, meaning clients can make that same call repeatedly while producing the same result

Method Description
GET retrieve resource representation/information only.
POST create new resources resources
PUT update existing resource
DELETE delete resources

Endpoints

... It's just a URL the API provider exposes and it forms the basis of reach request. ...

Examples

							
https://leadiq.sparkroom.com/Sparkroom
https://zpnoago855.execute-api.us-west-2.amazonaws.com/
https://graph.facebook.com/v3
							
						

Method Examples with the Endpoint assumed

							
GET: /users/12345

POST: /members/
	Payload: 
	member_id=12345
	member_name=Billy
	member_state=AZ

GET: /api/v2/weather/phoenix/az/85022

PUT /user/12345/name/billy

							
						

SOAP

... Simple Object Access Protocol ...

This is a protocol that uses XML as a format to transfer data. Its main function is to define the structure of the messages and method of communication. It also uses WSDL, or Web Services Definition Language, in a machine-readable document to publish a definition of its interface.

An example

							
POST /getMemberDetail HTTP/1.1
Payload: user_id=12345
Content-Type: application/soap+xml; charset=utf-8
							
						

Response

								
								
									Billy
									Neal
									Dumbass
									
13002 Phoenix Arizona

Ok, that's all find and dandy, but what's the difference between the two?

Difference between REST and SOAP

SOAP REST
It has strict rules and advanced security to follow. There are loose guidelines to follow allowing developers to make recommendations easily
It is driven by Function It is driven by Data
It requires more Bandwidth It requires minimum Bandwidth

DEFERO Likes RESTful API's

Where do we use them

... All over the place. That's where. ...

Add or Update subscribers inside MyEmma
(via FB Lead Ads)

Send some data from one system (Sparkroom)
to another system (DS)

Maybe someday we will send some OLO data to MyEmma

enough with the diagrams!!

Ok, here's some code that sends member data to Qples so they can print some killer coupon...

							
$data = array(
                "couponID" => "3468",
                "token" => "YOUR_UNIQUE_USER_TOKEN",
                "apiKey" => "XYQPLESMUYT6ZRTY4W328f62769c5e3c7dc3e58f5a4d1ea399"
             );

$url_send ="https://mv7izvuqle.execute-api.us-east-1.amazonaws.com/Production/push-to-external";
$str_data = json_encode($data);

$ch = curl_init($url_send);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                      
    'Content-Type: application/json',                                                                            
    'Content-Length: ' . strlen($str_data))                                                                       
); 

$result = curl_exec($ch);
curl_close($ch); 
							
						

THE END

... Thanks Everyone!!