Consuming External APIs in Micronaut

So far, we’ve learned how to create REST APIs in Micronaut and return JSON. But in real-world applications, you often need to consume external APIs.

For example:

  • Fetching weather information.
  • Calling payment gateways.
  • Getting stock market data.
  • Consuming other microservices inside your company.

The good news:
Micronaut makes this task very simple using @Client and @Get.

In this post, we will learn step-by-step how to call external APIs in Micronaut.

Consuming External APIs in Micronaut

What is @Client?

The @Client annotation allows you to declare an HTTP client interface:

  • Micronaut automatically generates the code to make HTTP calls.
  • It handles serialization and deserialization.
  • You only need to define methods with annotations like @Get, @Post, etc.

Key takeaway:

You describe what you want to call, Micronaut handles how to call it.

Scenario Example: Fetching a User from an External API

Let’s assume we want to call an external API that returns user details:

Sample response:

Step 1: Create a New Micronaut Project

Use Micronaut CLI or Micronaut Launch.

Step 2: Add HTTP Client Dependency

In your pom.xml, verify you have:

Micronaut needs this dependency to enable HTTP Client features.

Step 3: Create a Model Class

This will match the response structure of the external API.

✅ Now Micronaut can automatically map the JSON response into this Java object.

Step 4: Create the HTTP Client Interface

Here comes the power of @Client.

✅ Explanation:

  • @Client: defines the base URL.
  • @Get: specifies the endpoint and path parameter.
  • Micronaut auto-generates the implementation at compile time.

Step 5: Create a Controller to Test

Now let’s build a simple controller to test the external API call.

✅ When you call http://localhost:8080/local/users/1, Micronaut:

  • Invokes your external API client (UserClient).
  • Calls https://jsonplaceholder.typicode.com/users/1.
  • Converts JSON to User Java object.
  • Returns it as JSON to the caller.

Step 6: Run and Test

Start your application:

mvn mn:run

Test using curl or browser:

http://localhost:8080/local/users/1

Expected result:

🎉 Success — you just consumed an external API!

Additional Tip: Configurable Base URL

Instead of hardcoding the URL inside @Client, you can also configure it in application.yml.

application.yml

Client Interface

✅ This allows you to easily change the URL for different environments (dev, prod, etc.).

Summary Table

Micronaut FeatureWhat it does
@ClientDeclares HTTP Client
@GetSpecifies GET endpoint
Automatic MappingConverts JSON to Java
ConfigurationAllows flexible URLs

Conclusion

  • ✅ Consuming external APIs in Micronaut is super easy.
  • ✅ You don’t need to write complex HTTP client code.
  • ✅ Micronaut automatically maps JSON to Java.
  • ✅ You just define the interface and annotations.

👉 Even complete beginners can build powerful API clients using Micronaut with minimal code.

Further Reading

  1. Micronaut HTTP Client Documentation
    https://docs.micronaut.io/latest/guide/#httpClient
  2. Micronaut @Client Annotation
    https://docs.micronaut.io/latest/api/io/micronaut/http/annotation/Client.html
  3. JSONPlaceholder Free API (used in this example)
    https://jsonplaceholder.typicode.com/