Gleam brings static types and functional purity to the BEAM VM — the same runtime as Erlang and Elixir. UUID v7 fits Gleam’s design: a pure function call returns an ID with no global state, no coordination, and embedded creation time. See What is a UUID for the RFC 9562 specification.

gleam.toml dependency

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
gleam_uuid  = ">= 1.0.0 and < 2.0.0"
wisp        = ">= 0.14.0 and < 2.0.0"
gleam_http  = ">= 3.6.0 and < 4.0.0"

Generate UUID v7

import gleam_uuid as uuid

pub fn generate_id() -> String {
  uuid.v7()
  |> uuid.to_string
}

// Extract timestamp from UUID v7
pub fn created_ms(id: String) -> Result(Int, Nil) {
  use parsed <- result.try(uuid.from_string(id))
  let hex = string.replace(uuid.to_string(parsed), "-", "")
  let prefix = string.slice(hex, 0, 12)
  int.parse_base(prefix, 16)
}

Wisp HTTP handler

import gleam/http/request.{Request}
import gleam/http/response.{Response}
import gleam/json
import gleam_uuid as uuid
import wisp

pub type Order {
  Order(id: String, customer_id: String, total: Float)
}

pub fn create_order(req: Request) -> Response {
  use body <- wisp.require_json(req)

  let id = uuid.v7() |> uuid.to_string

  let order = case body {
    json.Object(fields) -> {
      use customer_id <- result.try(json.get_string(fields, "customer_id"))
      use total       <- result.try(json.get_float(fields, "total"))
      Ok(Order(id: id, customer_id: customer_id, total: total))
    }
    _ -> Error(Nil)
  }

  case order {
    Ok(o) ->
      json.object([
        #("id", json.string(o.id)),
        #("customer_id", json.string(o.customer_id)),
        #("total", json.float(o.total)),
      ])
      |> json.to_string_builder
      |> wisp.json_response(201)

    Error(_) ->
      wisp.bad_request()
  }
}

Cursor pagination with UUID v7 ordering

// UUID v7 lexicographic order = chronological order
// Use string comparison for cursor pagination
pub fn list_orders(cursor: Option(String), limit: Int) -> List(Order) {
  case cursor {
    None ->
      db_query("SELECT id, customer_id, total FROM orders ORDER BY id LIMIT $1", [limit])
    Some(c) ->
      db_query(
        "SELECT id, customer_id, total FROM orders WHERE id > $1 ORDER BY id LIMIT $2",
        [c, limit],
      )
  }
}

UUID v7 on the JavaScript target (FFI)

// uuid_ffi.gleam — FFI for Gleam JS target
@external(javascript, "./uuid_ffi.mjs", "uuidv7")
pub fn v7_js() -> String

// uuid_ffi.mjs
// import { v7 as uuidv7 } from 'uuid';
// export function uuidv7() { return uuidv7(); }

Further reading

External references

Frequently asked questions

How do I generate UUID v7 in Gleam?

Use the gleam_uuid package. Add it to your gleam.toml dependencies and call uuid.v7(). The function returns a Uuid type; use uuid.to_string(id) for the hyphenated string form. See What is a UUID for the specification.

Can Gleam UUID v7 run on the Erlang and JavaScript targets?

gleam_uuid targets the Erlang BEAM runtime. For the JavaScript target (Deno, Node.js, browsers), use a JavaScript UUID library via FFI: @external(javascript, "./uuid_ffi.mjs", "uuidv7") and call the npm uuid package. See UUID v7 in JavaScript.

How does UUID v7 work with Gleam result types?

Gleam's type system makes UUID v7 extraction safe. uuid.from_string(s) returns Result(Uuid, Nil) — you must handle the error case explicitly. This prevents silent ID truncation or invalid parse bugs common in dynamic languages. See UUID v4 vs UUID v7.