Consume your First API | Chatbot Library
Skip to main content

Consume your First API

Let's consume our first Api from our Chatbot

Or Look at this Tutorial in Spanish with Mizar.

Why Apis?

Apis makes Applications better, faster and its a common requirement on common days. Your chatbot can Use Apis to get helpfull information, make Payments, book tables or schedule with a Professional.

Calling APIS

In order to call APIs we are going to use a package from composer(php package manager) called GuzzleHTTP, this package is like Axios(for Node.js/npm) but with other structure.

When downloading guzzle http, you should have a folder named vendor when all the composer packages are in. My suggestion is to put this folder at the same level that your chatbot Folder. but you can put it wherever you like.

First take care of require this on our coreChatbot file.

require_once("../vendor/autoload.php");

Using Guzzle HTTP

Right now we are going to call a simple api. For more information about GuzzleHTTP look HERE

For this example we are going to call ipify API, you can login and read docs HERE

Inicializing Guzzle

To initialize the Client I recommend you to put this at the top of the Intent file when you are going to call the API

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

Then, create an Instance of the Client

$client = new Client();

After that we have to use a method of the GuzzleHTTP instance to retrieve Data.

$handler = $client->get(
"https://geo.ipify.org/api/v1?apiKey=YOURAPIKEY",
[
RequestOptions::HEADERS => [
'Accept' => 'application/json',
]
]
);

At the top we are using a method GET then we are setting the endpoint of API(url), As you see we are requesting with a header a response type of application/json

$result = $handler->getBody()->getContents();

Right here we are saving the content of the API call, the format should be a JSON, so you can parse it with json_decode to convert it into an associative/native array

After getting the variables that we need, you can output in your chatbot response to display whatever you like.

Keep an Eye

If you got haven't got anything, or an Error.

  • Check at the Documentation and try to emulate its execusion outside of the bot. If you got it work, lets check two options above.
  • Look at the Tutorial Again or check the Video Tutorial
  • Check the Troubleshooting Section