Building a Fast and Simple Emoji API using .NET Minimal APIs

Learn how to build a simple Emoji API in .NET 8 Minimal APIs, using a JSON file. The API includes endpoints to get all emojis, search by ID, and filter emojis by name with a flexible "contains" search

  1. Create a new Minimal API Project: First, create a new .NET project.

    dotnet new web -n EmojiApi
    cd EmojiApi
  2. Add the JSON file: Download the emojis.json file from this linkarrow-up-right and save it in the root of your project (same directory as Program.cs).

  3. Write the code in Program.cs:

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Text.Json;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

// Define a model for the emoji data
public class Emoji
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Symbol { get; set; }
}

// Load the emojis data from the JSON file
List<Emoji> emojis = LoadEmojis();

// Load emojis from the JSON file
List<Emoji> LoadEmojis()
{
    var filePath = Path.Combine(AppContext.BaseDirectory, "emojis.json");
    var jsonString = File.ReadAllText(filePath);
    var emojiList = JsonConvert.DeserializeObject<List<Emoji>>(jsonString);
    return emojiList;
}

// Get all emojis
app.MapGet("/emojis", () =>
{
    return Results.Ok(emojis);
});

// Get emoji by Id
app.MapGet("/emojis/{id}", (string id) =>
{
    var emoji = emojis.FirstOrDefault(e => e.Id.Equals(id, StringComparison.OrdinalIgnoreCase));
    return emoji is not null ? Results.Ok(emoji) : Results.NotFound();
});

// Filter emojis by name (contains style search)
app.MapGet("/emojis/filter", (string name) =>
{
    var filteredEmojis = emojis.Where(e => e.Name.Contains(name, StringComparison.OrdinalIgnoreCase)).ToList();
    return filteredEmojis.Any() ? Results.Ok(filteredEmojis) : Results.NotFound();
});

app.Run();

Explanation:

  1. Model Definition:

    • A simple Emoji class with Id, Name, and Symbol properties to represent the data structure in the JSON file.

  2. Loading JSON Data:

    • The JSON file is loaded into a list of Emoji objects using JsonConvert.DeserializeObject. Make sure you have the Newtonsoft.Json package installed in the project (dotnet add package Newtonsoft.Json).

  3. Endpoints:

    • GET /emojis: This endpoint returns all the emojis.

    • GET /emojis/{id}: This endpoint retrieves an emoji by its Id. It returns a 404 if not found.

    • GET /emojis/filter?name={searchTerm}: This endpoint performs a case-insensitive search to filter emojis based on whether the name contains the search term (similar to SQL’s LIKE operator).

Testing the API:

Once the API is running, you can test the following endpoints:

  • Get all emojis: GET /emojis

  • Get an emoji by ID: GET /emojis/{id}

  • Filter emojis by name: GET /emojis/filter?name=smile

Note:

Ensure that you have the necessary NuGet packages (Newtonsoft.Json) for JSON deserialization, as shown below:

This project will meet the specifications you've mentioned and allow you to interact with emoji data from a JSON file via a minimal API.

Last updated