How I Used Forem API to Fetch Dev.to in Laravel 9
Tags: api, forem, laravel, tutorial
Published: 2022-03-10 18:31:07

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:

{% vimeo 685866140 %}

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](https://swiperjs.com/). In case you would like to know how I used Swiper in Laravel just let me know 🗣️

| Table of Content                                 |
| ------------------------------------------------ |
| [1- What can you do with forem API?](#chapter-1) |
| [2- Getting started:](#chapter-2)                |
| [3- Connect to API:](#chapter-3)                 |
| [4- Next steps:](#chapter-4)                     |

### 1- What can you do with forem API?<a name="chapter-1"></a>

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**](https://developers.forem.com/api).

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

### 2- Getting started:<a name="chapter-2"></a>

First thing to do is to generate API key (obviously!). You can do that by following the steps in this [**link**](https://developers.forem.com/api#section/Authentication).

### 3- Connect to API:<a name="chapter-3"></a>

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.

```php
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);
});
```

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:<a name="chapter-4"></a>

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**](https://twitter.com/mmartin_joo/status/1497709380346781702) 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**](https://dev.to/moose_said)