Martin Joo

Martin Joo



💡How to integrate 3rd parties into your application? We must integrate with many 3rd party APIs and SDKs in every project. You can treat them as mini-applications inside your app. 🧵Keep Reading

1/14 Treat your 3rd parties as if they were mini-applications inside your application. They have their own namespaces DTOs service classes and so on. Something like this:

2/14 In the Services folder, I create a new folder for each 3rd party and treat it like it was a mini-application inside my app. It looks like this:

3/14 In Laravel, we have the services.php config file. This is the perfect place to configure our 3rd parties:

4/14 Each service can have its own service provider. This is the perfect opportunity to use Laravel’s singleton bindings. We can create a new instance using the configuration values:

5/14 Now everything is ready to actually implement the GumroadService class:

6/14 With this approach, we have one class for the whole API, and each endpoint has a method. This is perfectly fine and works 99% of the time. However, there are some situations when the API is simply too big to put everything in one class.

7/14 If you think about it, these methods such as the product() are requests. Why don’t we treat them as if they were form requests? Let’s put ‘em in separate classes:

8/14 Each request class implements one API endpoint:

9/14 But now we have to use every request separately. Which is fine, but not so convenient, so we can use these requests in the GumroadService:

10/14 To make our life even easier, we can use the laravel-transporter package written by @JustSteveKing With this package the GetProductRequest looks like this:

11/14 In the parent GumroadService we can configure the access token:

12/14 These requests can be used pretty easily:

13/14 That’s it! With laravel-transporter, you can easily write these requests with only 10 lines of code. It’s a pretty cool package!

14/14 Thank you if you’re still here! Two weeks ago I started a private Discord community where I’ll post exclusive content weekly. These examples come from a 28-page mini-book I’ll share next week only on Discord. You can join us here:

Follow us on Twitter

to be informed of the latest developments and updates!

You can easily use to @tivitikothread bot for create more readable thread!
Donate 💲

You can keep this app free of charge by supporting 😊

for server charges...