DEV Community

Dat One Dev
Dat One Dev

Posted on

API in Mini-Micro

Introduction

A few days ago, I published a blog titled What is an Application Programming Interface (API). I found it to be an interesting post.

Following that, I decided to write another blog.

How to implement an API in Mini Micro.

And that's exactly what we're going to do today.

Before we move in, I want to clarify one thing: we won’t be working with a high-level or complex API. Instead, we'll start simple and focus on the basics.

What is an Application Programming Interface (API)

I have already specifically covered this particular topic in this blog - πŸ‘‰What is an Application Programming Interface (API)

However, I thought providing a definition here would be a good idea.

Application Programming Interface (API) allows communication between 2 different software without the disclosure of important data.

API in Mini-Micro

What is Mini-Micro

Mini-Micro is a neo-retro virtual computer that uses Mini-Script programming language.

For more information on Mini-Script and Mini-Micro, check out these sources.

Which API would we use?

As I said I am going to use a very simple API, Thats why I decided to pick up Joke API.

I found this online and found it perfect for our project

-πŸ‘‰Joke API

This API generates a random joke every time the Hypertext Transfer Protocol(HTTP) request is called.

Understanding the randomly generated joke

You may have observed that:

  • Whenever you reload, a joke is randomly generated.
  • Joke is generated in JSON format.

Understanding the JSON

For someone that has little to know knowledge about JSON, This blog's published by me will help them.

-πŸ‘‰What is JSON?
-πŸ‘‰JSON in Miniscript

If you know JSON, understanding the joke generated on the site should be no issue for you.

Probably you have already observed that the generated JSON joke has 4 keys.

  • Tag
  • Setup
  • Punchline
  • Id

The main joke is in the setup and the punchline. So we would need to print them in Mini-Micro

But before printing them, we need to somehow get this JSON code from the web to Mini-Micro

Importing JSON from Web to Mini-Micro

There is a function in Mini-Micro known as http.get().

This function returns the content of the given URL. The http.get() function also supports images, sounds, text, and raw binary data.

Using this function, we can store the text content of the site in a variable like this

More detailed blog on http.get()

-πŸ‘‰Rendering a sprite from web using http.get()

response_json = http.get("https://official-joke-api.appspot.com/random_joke")
Enter fullscreen mode Exit fullscreen mode

JSON into Mini-Script

Now we can't directly print JSON code to the screen. We still somehow need to parse it into Mini-Script, so that we can get the value of keys setup and punchline, and only print them

To do this, first we would import the JSON library into our code, and then we would use JSON.parse() to convert the JSON data into Miniscript data.

This could be done like this

import "json"
response_json = http.get("https://official-joke-api.appspot.com/random_joke")
response = json.parse(response_json)
Enter fullscreen mode Exit fullscreen mode

More detailed post on parsing json into miniscript and vice versa

-πŸ‘‰JSON in Minscript

Printing the JOKE

Now we could just straight up print our joke easily like this

print response["setup"]      
print response["punchline"]
Enter fullscreen mode Exit fullscreen mode

And our code is done now, whenever this code is running a new joke gets printed

Outro

Now you know how to implement an API in Mini-Micro.

But now more question arises:

What is an API Key?
Is API and UI the same?

And that's what makes us humans the curious cat, but all the rest questions are talk of a different blog.

Till Then Stay Curious and Stay Selfish

πŸ‘‰Youtube

πŸ‘‰BlueSky

πŸ‘‰Discord

Top comments (1)

Collapse
 
fernando_noise profile image
Fernando • Edited

I did it!
It's extremely easy and very funny hehehe!
Image description