Consuming APIs In Laravel

Consuming APIs In Laravel  image

You've built a great app, but now your users want more power.

Maybe they want their monthly reports to be sent to their accounting software. Perhaps they need to automatically create new tasks in their project management tool, or they want contact form submissions to be automatically sent to their CRM.

There’s no doubt that integrating with third-party APIs and adding these features to your app is a great way to add value for your users, but it can also be super daunting to get started.

In fact, I’d go as far as to say API integrations are one of the most complex and scary things I’ve faced in my entire developer career! There are just so many things you’ve got to know if you want your API integration to work properly and be robust, secure and easy to maintain.

But even more frustrating is the fact it’s also just so damn hard to find reliable information you can trust.

Take a look online and you’ll come across a huge amount of information about APIs that’s either completely outdated or just plain wrong. If you’ve ever done any research yourself, you’ll know it’s often near impossible to know what’s still relevant, or exactly how some of the concepts you’re learning are supposed to fit into your application.

Theory is great - but what you really need is actionable information!

That's why I'm really excited to announce that my new book, Consuming APIs In Laravel, is now available to buy!

This book is a 440+ page, deep dive into the world of APIs. It's your complete guide to confidently building robust and powerful API integrations in your Laravel projects to add cool new features.

It’s the comprehensive API manual I wish I had when I started out as a developer. One you can trust and will want to refer back to again and again.

👉 Get 20% off the book with discount code: LN20

👉 The book is also available in a bundle (Consuming APIs in Laravel and Battle Ready Laravel). Get 10% off the bundle with discount code: LN10

👉 You can download a FREE sample of the book. The sample is from the Webhooks chapter and covers the Building Webhook Routes and Webhook Security sections.

You can buy the book or download your free sample at consuming-apis-in-laravel.com.

What's Covered In The Book?

The book aims to help you build powerful API integrations that are testable, maintainable, and easy to understand.

I'll show you the way I've built integrations for many projects that help my clients and their users, including specific code examples that give context as to exactly where, why, and how you’d use something in your Laravel app.

The book is broken down into five chapters:

Chapter 1: About APIs

This chapter covers the theory of APIs to give you a solid foundation to build upon. It covers things like:

  • ✅ Data formats: JSON vs. XML
  • ✅ HTTP message structures
  • ✅ Types of web APIs
  • ✅ Benefits and drawbacks of APIs
  • ✅ API authentication
  • ✅ API integration security

Chapter 2: Code Techniques

This chapter covers code techniques that I use to make my API integrations more robust and easier to maintain. It covers:

  • ✅ Strict type-checking
  • ✅ Composition over inheritance
  • ✅ Final classes
  • ✅ Data transfer objects
  • ✅ Readonly classes and properties
  • ✅ Using interfaces and the service containers
  • ✅ Redacting sensitive parameters
  • ✅ Enums

The techniques covered in this chapter don't just apply to API integrations though. You can use them anywhere in your Laravel apps.

Chapter 3: Building an API Integration Using Saloon

We'll be covering how to consume APIs using Saloon v3, an awesome PHP package built by Sam Carré that has over 500k downloads!

Saloon provides a simple, fluent, and object-oriented approach for consuming APIs, making it easy for you to build API integrations without having to worry about the underlying implementation.

Inside, we'll take a look at what Saloon is, the benefits of using it, and alternative approaches that you may want to use. We'll then step through and reveal exactly how you can confidently use Saloon in your Laravel applications.

We'll cover:

  • ✅ Alternatives to Saloon
  • ✅ API authentication
  • ✅ Sending requests and parsing responses
  • ✅ Pagination
  • ✅ Solo requests
  • ✅ Sending concurrent requests
  • ✅ Middleware
  • ✅ Plugins
  • ✅ Error handling
  • ✅ Retrying requests
  • ✅ Handling API rate limits
  • ✅ Caching API responses
  • ✅ Testing API integrations

In particular, we'll be putting a focus on the testability of your code. By the end of this chapter, you'll also be able to write tests for your API integrations with ease.

Chapter 4: OAuth

OAuth is a scary topic for a lot of developers! There are so many moving parts and it can be hard to know where to start.

In this chapter, we're demystifying OAuth and covering:

  • ✅ Use cases for OAuth
  • ✅ OAuth terminology
  • ✅ Breakdown of each OAuth 2.0 flow
  • ✅ Benefits and drawbacks of OAuth
  • ✅ OAuth security & best practices
  • ✅ Integrating with an OAuth API using Saloon
  • ✅ Testing OAuth integrations

We'll then take a look at how you can use Saloon to build an OAuth integration with Spotify. This includes learning how to write tests for your OAuth integration to give you confidence that it works as expected.

Chapter 5: Webhooks

Finally, we'll then dive into the world of webhooks. We'll cover what webhooks are, how they work, and how to securely handle webhooks in your Laravel applications.

We'll cover:

  • ✅ What webhooks are and how they work
  • ✅ Advantages and disadvantages of webhooks
  • ✅ Webhook security & best practices
  • ✅ Securely webhooks in Laravel
  • ✅ Testing webhook routes
  • ✅ Using Laravel's queue system to improve your webhook performance

Who Is This Book For?

If your client or boss uttering the words “we need it to integrate with XYZ’s API” gives you nightmares, this book is for you.

Likewise, if you've never been sure how to structure your API integrations, or you've struggled to write tests for them, this book is for you.

The book doesn't just show you how to write code. It also focuses on teaching you skills to improve the standardisation, maintainability and testability of your code. It teaches you techniques that you can use to improve your code quality and make your API integrations more robust and easier to maintain.

It also shows you how to write tests for your API integrations so you can have confidence that your code works as expected.

By the end of this book, you'll be able to confidently build powerful API integrations for your Laravel applications.

Pricing

The book is available for $39. You will get:

  • ✅ PDF (light mode)
  • ✅ PDF (dark mode)
  • ✅ EPUB

You can use the discount code LN20 to get 20% off your purchase. This brings the price down to $31.20!

The book is also available in a bundle for $59. You will get:

  • ✅ Consuming APIs In Laravel - PDF (light mode)
  • ✅ Consuming APIs In Laravel - PDF (dark mode)
  • ✅ Consuming APIs In Laravel - EPUB
  • ✅ Battle Ready Laravel - PDF (light mode)
  • ✅ Battle Ready Laravel - PDF (dark mode)
  • ✅ Battle Ready Laravel - EPUB

You can use the discount code LN10 to get 10% off this bundle as well. This brings the price down to $53.10!

👉 There's also a FREE sample of the book. The sample is from the Webhooks chapter and covers the Building Webhook Routes and Webhook Security sections.

👉 You can get your copy at: consuming-apis-in-laravel.com.

About the Author

I'm Ash Allen (@AshAllenDesign on Twitter/X).

I'm a Laravel web developer from the UK.

I've worked with many companies around the world to help build and improve Laravel applications they can be proud of!

I wrote "Battle Ready Laravel" which has sold over 750+ copies and is available as a PDF, EPUB, and a paperback. I've also written "The Clean Coder's Guide to Laravel" which has been downloaded more than 6k times, and I have an active blog where I write about Laravel and PHP. I've also had articles published on the Laravel News, Vonage, and Honeybadger blogs.

I have several PHP packages (such as Short URL and Laravel Exchange Rates) which have been installed over 610k times. I also love working on other open-source projects and have contributed to the Laravel framework.

Need a Little More Convincing?

Here's what some of the early readers have said about the book:

"This book is a treasure trove of knowledge and practical ways to integrate with third party APIs. Ash has a brilliant skill of breaking down complex integration methods and RFC specification into beautiful, readable and testable code." - Sam Carré, Creator of Saloon, Head of Engineering at Plannr CRM

"If you work on a Laravel application and that application hooks into an API such as Stripe, GitHub, HubSpot etc... then this book is absolutely for you. There's a lot of great content and it's changed how I plan on approaching handling APIs in the future" - Jon Purvis, DevOps Engineering Lead