DEV Community

Cover image for A simple guide to using the Useless Facts API
Kaushal Sharma
Kaushal Sharma

Posted on • Updated on

A simple guide to using the Useless Facts API

So, over the course of two weeks filled with pain and despair due to sickness, I created a simple API in django rest framework
to learn about itself and also apply concepts like Token Authentication, Scoped Rate Limiting, and how to document APIs to name a few.

Now, realizing that getting data is not so cheap and also scraping someone else's website can put unnessesary load on their website
and their wallet, I'm making whatever data I've collected available to everyone else through these APIs.

Steps to using these APIs

Step 1: Call the /auth/login/ endpoint, or /auth/registration/ if you are a new user, to get back a Token

Note- All demonstration done using httpie, but you can see how to do it using curl in the swagger doc here

http POST https://uselessfacts.fly.dev/auth/login/ username=<your-registered-username> password=<your-registered-password>
Enter fullscreen mode Exit fullscreen mode

OR

http POST https://uselessfacts.fly.dev/auth/registration/ username=<your-choice-of-username> password1=<your-choice-of-password>  password2=<same-as-password1>
Enter fullscreen mode Exit fullscreen mode

You'll get back the following response -

{
    "key": "c596a03fd15783e14d512c843a7ee4ba84ea77eb"
}
Enter fullscreen mode Exit fullscreen mode

This is the token we were talking about

Step 2: Send this Token as Authorization while calling the v1 APIs to get back the useless facts!

http POST https://uselessfacts.fly.dev/v1/random-fact/ 'Authorization: Token c596a03fd15783e14d512c843a7ee4ba84ea77eb'
Enter fullscreen mode Exit fullscreen mode

OR

http POST https://uselessfacts.fly.dev/v1/fact/145/ 'Authorization: Token c596a03fd15783e14d512c843a7ee4ba84ea77eb'
Enter fullscreen mode Exit fullscreen mode

And upon successful request you'll get back the useless fact like below

{
    "data": {
        "fact": "California consumes more bottled water than any other product.",
        "id": 145
    },
    "status": "success"
}
Enter fullscreen mode Exit fullscreen mode

You can check out other respones in the swagger documentation here

Now the most important step, like a good person make sure you logout as well!

Step 3: To logout call the /auth/logout/ endpoint with no body with sending the Token in authorization just like we did in the previous step

http POST https://uselessfacts.fly.dev/auth/logout/ 'Authorization: Token c596a03fd15783e14d512c843a7ee4ba84ea77eb'
Enter fullscreen mode Exit fullscreen mode

Upon successful request you should get the following response -

{
    "detail": "Successfully logged out."
}
Enter fullscreen mode Exit fullscreen mode

You can explore all the other available endpoints as well as the models in the swagger documentation mentioned above.

You can get in touch with me here or on LinkedIn OR Instagram !

Top comments (0)