DEV Community

Cover image for How I Used Forem API to Fetch Dev.to in Laravel 9
Mostafa Said
Mostafa Said

Posted on

How I Used Forem API to Fetch Dev.to in Laravel 9

As some of you know, I'm in the process of building my own blog which I will feature all my blog posts on it here on Dev community. I'm using Laravel 9 and I couldn't find any useful resources or descriptive documentation on how to use Forem API with Laravel.
You can actually use the API in Laravel without having to use Curl or anything other than Laravel itself.

As you can see below I'm using Swiper to loop over my articles from Dev.to in my website:

I will not talk much about Swiper for now as this is outside of this article scope but you can read more about it from HERE. In case you would like to know how I used Swiper in Laravel just let me know šŸ—£ļø

1- What can you do with forem API?

Forem API is really powerful and you can do a lot of stuff with it. You can fetch almost any data on Dev.to such as articles, comments, followers, reactions and you can also post articles from your own website to Dev community. You can read all about it from their awesome documentation.

  • In case the link didn't work try running any free VPN extension in you browser and it will work.

2- Getting started:

First thing to do is to generate API key (obviously!). You can do that by following the steps in this link.

3- Connect to API:

Now let's head to routes/web.php in your Laravel project and to be honest I'll be treating this area as my playground.

What you can do is to create a testing ping point to see if you're receiving the data correctly.

  • Use the below code to retrieve all your articles from Dev.to
  • Don't forget to import use Illuminate\Support\Facades\Http; on the top.
  • Replace "YOUR API KEY" with your API key.
route::get('/ping', function () {

    $articles = Http::withHeaders([
        'api-key' => "YOUR API KEY",
    ])->get('http://dev.to/api/articles/me/published');

    $articles = json_decode($articles, true);

    ddd($articles);
});
Enter fullscreen mode Exit fullscreen mode

And there you go, if you visit YourWebURL/ping you will be able to see Laravel's ddd is doing it's magic and organizing the data we're receiving in an elegant readable way.

4- Next steps:

Possibilities are endless really! You can do whatever you like with these data. I prefer creating PHP class for DTO (DataTransferObjects) and collect the data and store it the way I want. You can read more about DTOs from HERE by Martin Joo.

Thank you all for reading and if any questions please let me know and I will answer as soon as possible šŸ‘‹ If you like this article and want more about Laravel, Vue and Tailwind you can just follow me

Top comments (0)