CoAP · Study deck

CoAP API Design: Resource and Response Contracts

A field sensor can expose one value through several CoAP methods.

Broker Bex is your guide for this deck.

Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Design RESTful CoAP APIs: Construct resource hierarchies using nouns with proper URI conventions and versioning strategies
  • Distinguish Content Formats: Compare JSON, CBOR, and text/plain payloads and select the appropriate format for constrained versus development environments
  • Implement Error Handling: Apply proper CoAP response codes (2.xx/4.xx/5.xx) and construct helpful error payloads for production systems
  • Configure Security Controls: Configure DTLS, rate limiting, and per-device access control for production CoAP deployments
iotclass.org

Major section

In 60 Seconds

A useful interface must tell the operator what can be read, what can change, and what happens when a request arrives twice.

  • An application programming interface means a named way for programs to exchange requests and results; it is called an API.
  • A protocol means shared rules for an exchange.
  • A payload means the useful data inside a message.

Key terms

Transport layer security
Transport layer security means protection for a network exchange.
iotclass.org

Major section

In 60 Seconds (continued)

Constrained Application Protocol (CoAP) means a compact request method for small devices.

  • JavaScript Object Notation (JSON) means a text format built from named values.
  • A sampling rate means how often a sensor takes a reading.
  • A gateway means the bridge between local devices and a wider network.
  • Transport layer security means protection for a network exchange.
iotclass.org

Major section

In 60 Seconds (continued)

Each result should state what changed, what did not, and who can try again.

  • This runway does not finish the interface or prove its security.
  • The deeper material turns the menu into resource names, methods, formats, timing rules, and release tests.
  • Proper response codes (2.xx/4.xx/5.xx), DTLS security, and rate limiting are essential for robust production IoT APIs.
iotclass.org

Major section

Phoebe's Field Notes: Separate Annual Charge from Pulse Survival

The mathematical gist.: The chapter's 525,600 yearly messages turn 0.278 µAh CON and 0.139 µAh NON charges into about 190 and 117 mAh/year after adding 43.8 mAh of background use.

  • A 220 mAh cell therefore gives about 1.16 and 1.88 years, a 62.5% NON advantage.
iotclass.org

Major section

For Beginners: CoAP API Design

Designing a CoAP API means deciding how IoT devices expose their data and accept commands.

  • Think of it as creating a menu for your sensor -- you define endpoints like /temperature or /status that other devices can read.
iotclass.org

Major section

Building Sammy's Data Menu

"I want other devices to read my temperature, but I also want them to set my sampling rate," said Temperature Terry. "How do I organize all that?".

  • Your root is /sensor, and under it you have /sensor/temperature for reading data and /sensor/config for settings.
  • Every byte saved is energy saved!".
iotclass.org

Major section

Building Sammy's Data Menu (continued)

When someone sends a GET to /sensor/temperature, they get your latest reading.

  • When they send a PUT to /sensor/config, they can change your sampling rate.".
  • Some devices want your data in JSON, others in CBOR -- which is like compressed JSON.
  • Your API should let the requester choose the format using the: Accept option.
iotclass.org

Major section

Minimum Viable Understanding: CoAP REST Design

Core Concept: CoAP follows REST principles - design resources as nouns, use HTTP-like methods as verbs.

  • application/octet-stream (42) - raw binary only when both sides already share the schema.

Why it matters

Why It Matters: Consistent RESTful design makes APIs intuitive for developers, enables caching, supports proxying, and allows standard tooling to work seamlessly.

iotclass.org

Major section

Error Handling

4.00 Bad Request - request syntax or payload was invalid.

  • 4.01 Unauthorized - authentication is required.
  • 4.04 Not Found - resource does not exist.
  • 4.05 Method Not Allowed - method does not apply to that resource.
iotclass.org

Deck summary

Key takeaways

A useful interface must tell the operator what can be read, what can change, and what happens when a request arrives twice.

  • Constrained Application Protocol (CoAP) means a compact request method for small devices.
  • Each result should state what changed, what did not, and who can try again.
  • The mathematical gist.: The chapter's 525,600 yearly messages turn 0.278 µAh CON and 0.139 µAh NON charges into about 190 and 117 mAh/year after adding 43.8 mAh of background use.
  • Designing a CoAP API means deciding how IoT devices expose their data and accept commands.
iotclass.org

Retrieval practice

Recall check 1 of 2

Broker Bex says: answer from memory, then check your reasoning.

Q1A CoAP API exposes /config and /temp resources on a constrained sensor. Which design choice is most reviewable?

ALong paths that explain the sensor's purpose.
BOne generic error code with detailed text.
CShort paths with clear methods and formats.
DLeave Observe rate limiting undefined until the first notification flood
Show answer

Answer: C CoAP API design reviews resource shape, payload format, security controls, reliability choice, and operational limits together.

iotclass.org

Retrieval practice

Recall check 2 of 2

Broker Bex says: answer from memory, then check your reasoning.

Q2A battery-powered temperature sensor transmits readings every 5 minutes to a cloud gateway. The payload includes device ID, temperature value, unit, and timestamp. Which content format should be selected for production deployment, and what is the primary reason?

Aapplication/cbor (Content-Format 60) - binary encoding reduces payload size by approximately 40-50% compared to JSON.
Bapplication/json (Content-Format 50) - JSON is universally supported and human-readable, making it the safest production choice
Ctext/plain (Content-Format 0) - the simplest format, sending only the raw numeric value to minimize bytes
Dapplication/octet-stream (Content-Format 42) - raw binary is always the most efficient encoding
Show answer

Answer: A CBOR (Content-Format 60) is the standard production choice for constrained IoT devices because it preserves JSON's flexible data model (objects, arrays, strings, numbers) while encoding them in a compact binary form that is 40-50% smaller than JSON.

iotclass.org

Print reference

Answers

Answer key.

  1. C · CoAP API design reviews resource shape, payload format, security controls, reliability choice, and operational limits together.
  2. A · CBOR (Content-Format 60) is the standard production choice for constrained IoT devices because it preserves JSON's flexible data model (objects, arrays, strings, numbers) while encoding them in a compact binary form that is 40-50% smaller than JSON.
iotclass.org