Using HttpClient Accessor API

Avatar of Hemanta SundarayHemanta Sundaray

The Accessor API provides a shortcut that skips the manual service access step.

Instead of writing yield* HttpClient.HttpClient first, you call the method directly on the HttpClient module:

http.ts
import { FetchHttpClient, HttpClient } from "effect/unstable/http";
import { Effect } from "effect";
function fetchUser(userId: number) {
// Using accessor API. No need for Effect.gen to access the client
return HttpClient.get(`https://dummyjson.com/users/${userId}`).pipe(
Effect.andThen((response) => response.json),
Effect.provide(FetchHttpClient.layer),
);
}
// Test it
Effect.runPromise(fetchUser(2)).then((user) => {
console.log("Name:", user.firstName, user.lastName);
});

Output:

Name: Michael Williams

Sign in to save progress

Stay in the loop

Get notified when new chapters are added and when this course is complete.