0Pricing
Jetpack Compose Academy · Lesson

Define a Retrofit API Interface

Declare endpoints with annotations.

Define a Retrofit API Interface is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Retrofit Does

Retrofit turns a plain Kotlin interface into a working HTTP client, so you describe your API and it writes the networking code for you.

It Starts With an Interface

You declare each endpoint as a function inside a Kotlin interface. Retrofit reads the annotations and builds the real implementation at runtime.

interface UserApi {
    // endpoint functions go here
}

Mark a GET Request

The @GET annotation tells Retrofit this function fetches data. You pass the path that sits after your base URL. 🌐

@GET("users")
fun getUsers(): Call<List<User>>

POST, PUT and DELETE Too

Beyond GET, Retrofit offers @POST, @PUT, and @DELETE so one interface can read and write data on your server.

@POST("users")
fun createUser(@Body user: User): Call<User>

Path Parameters

Use @Path to inject a value into the URL. Retrofit swaps the named placeholder in braces for the argument you pass in.

@GET("users/{id}")
fun getUser(@Path("id") id: Int): Call<User>

Query Parameters

Add @Query for values after the question mark, like search terms or page numbers. Retrofit appends them to the URL for you.

@GET("search")
fun search(@Query("q") term: String): Call<Result>

The Return Type

The function's return type tells Retrofit what shape to expect back. A data class here maps straight onto the JSON response.

@GET("users/{id}")
fun getUser(@Path("id") id: Int): Call<User>

Set the Base URL

You build one Retrofit instance with the base URL. Every endpoint path you declared is joined onto it automatically.

val retrofit = Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .build()

Create the Implementation

Call create with your interface class and Retrofit returns a ready-to-use object. You never write the request code yourself.

val api = retrofit.create(UserApi::class.java)

Add a Converter Factory

A converter factory teaches Retrofit how to turn JSON into your data classes. You add it once when building the instance.

.addConverterFactory(MoshiConverterFactory.create())

Don't Forget the Permission

To reach the network, declare the INTERNET permission in your manifest. Without it, every request fails before it even starts.

<uses-permission android:name="android.permission.INTERNET" />

Quick Check

Which annotation injects a value directly into the URL path?

Recap: Your API Interface

You described endpoints as annotated interface functions, set a base URL, added a converter, and let Retrofit build the client. Next you'll parse the JSON it returns.

Frequently asked questions

Is the “Define a Retrofit API Interface” lesson free?

Yes — the full text of “Define a Retrofit API Interface” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Define a Retrofit API Interface”?

Declare endpoints with annotations. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Define a Retrofit API Interface” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Define a Retrofit API Interface
  2. Parsing JSON with Moshi
  3. suspend Calls in the ViewModel
  4. Loading, Success & Error UI
← Back to Jetpack Compose Academy