Code on Demand? REST Constraint Explained
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
Code on demand is one of the five optional constraints of the REST architectural style, and it is the only one that is optional. It allows a server to send executable code, such as JavaScript, to a client, extending the client’s functionality without requiring a new version to be deployed.
Background: Where the Term Comes From
The term comes from Roy Fielding’s 2000 doctoral dissertation, Architectural Styles and the Design of Network-based Software Architectures, which introduced REST (Representational State Transfer). Fielding described REST as a set of six constraints: client-server, statelessness, cacheability, a uniform interface, a layered system, and code on demand. The first five are required for a system to be considered RESTful; code on demand is the one exception. Fielding explicitly wrote that it is the only optional constraint, meaning a service can be fully RESTful without ever using it.
Fielding also noted that code on demand reduces visibility, which is why it is optional. When a server sends executable code, the client cannot know in advance what that code will do. That adds risk, which is why many REST APIs choose not to use it. The constraint is often described as a way to “extend the client” after it has already been deployed, but it is rarely the default choice in modern API design.
How Code on Demand Works
In a typical REST request, the server returns data—usually JSON or XML—and the client decides how to render it. With code on demand, the server returns executable code along with, or instead of, data. The client then runs that code. The most common example is JavaScript served to a web browser. A server might return a small script that updates part of the page, rather than sending raw data and expecting the browser to have built-in logic for that update.
Other examples include Java applets, Flash animations, and even server-side scripts that are interpreted on the client. In all cases, the key is that the client did not have that capability before receiving the code. The server is effectively pushing new behavior to the client at runtime.
In practice, this is rarely done with pure REST APIs. Instead, it is more common in web pages that load JavaScript files from a CDN. Those files are not part of the page’s original code, but the browser executes them because the server instructed it to. That is code on demand in action, even if the API itself is not using the constraint.
When to Use It: Benefits and Trade-offs
The main benefit of code on demand is that it reduces the need for client-side updates. If a server can send new logic, the client does not need to be redeployed to gain new features. This is especially useful for web browsers, where users may be running old versions of a site’s JavaScript. The server can send updated code that works with the old browser, avoiding a forced upgrade.
Another benefit is that it can reduce the amount of data transferred. Instead of sending a large dataset and letting the client process it, the server can send a small script that processes the data on the client, then sends back only the result. This is similar to the idea of “moving computation to the data,” which is common in distributed systems.
However, the trade-offs are significant. Code on demand reduces visibility and makes caching harder. A proxy or a client cannot know what a piece of code will do, so it cannot cache the response safely. It also introduces a security risk, because executing arbitrary code from a server is dangerous. The client must trust the server completely, and even then, a compromised server could send malicious code to every client.
For these reasons, most REST APIs avoid code on demand. They prefer to send data and let the client handle the logic. This keeps the system simpler, more predictable, and easier to secure. Code on demand is best reserved for cases where the client is a web browser and the code is JavaScript, which is already a common and trusted execution environment.
Common Misconceptions and Mistakes
A common misconception is that code on demand is required for REST. It is not. Fielding was explicit that it is optional. A service that never sends executable code is still RESTful, as long as it satisfies the other five constraints. Many developers assume that sending JavaScript from a server automatically makes an API “more RESTful,” but that is incorrect.
Another misconception is that code on demand is the same as returning a URL to a script. Returning a link to a JavaScript file is not code on demand unless the client executes that script. Merely providing a link is just data. The constraint is about the server sending the code itself, not about referencing it.
A related mistake is mixing code on demand with server-side rendering. In server-side rendering, the server generates HTML and sends it to the client. That is not code on demand, because the HTML is not executable code. It is just data that the client renders. Code on demand specifically involves sending something that the client runs, such as JavaScript or a Java applet.
Finally, some developers confuse code on demand with the idea of “dynamic” APIs that change their responses based on client requests. That is not code on demand. The constraint is about the type of content sent, not about whether the server’s response varies. A REST API can be fully dynamic without ever sending executable code.
Code on Demand vs. Other REST Constraints
To understand code on demand, it helps to contrast it with the other constraints. The client-server constraint separates the user interface from the data storage. Statelessness means each request must contain all the information needed to process it, with no server-side session state. Cacheability requires that responses can be marked as cacheable or not. The uniform interface is the most complex constraint, requiring that resources are identified by URIs, manipulated through representations, and self-descriptive messages. The layered system allows intermediaries like proxies and gateways to be inserted without the client knowing.
Code on demand is the only constraint that involves sending executable content. The others are all about data and its representation. This is why it is often seen as a separate concern. It is also why it is optional: the other constraints are about making the system scalable and reliable, while code on demand is about extending functionality, which is a nice-to-have rather than a requirement.
In practice, a REST API that uses code on demand must still satisfy the other constraints. For example, the code sent must be self-descriptive and must not rely on server-side session state. That is a challenge, because executable code often needs context. The server must either embed all context in the code or rely on the client to provide it, which can be awkward.
Frequently Asked Questions
Is code on demand required for REST?
No. It is the only optional constraint in Fielding’s definition. A system can be fully RESTful without ever using code on demand. In fact, most REST APIs do not use it, because the security and caching trade-offs are not worth the benefit for most applications.
What is a real-world example of code on demand?
The most common example is a web page that includes a <script> tag. The server sends the JavaScript code, and the browser executes it. The browser did not have that code before the request, so the server has extended the client’s functionality. That is code on demand in action.
Why is code on demand optional?
Fielding made it optional because it reduces visibility. When a client receives executable code, it cannot know in advance what that code will do. That makes the system harder to debug, harder to cache, and riskier to use. The other constraints are about making the system reliable and scalable; code on demand is about flexibility, which is not always worth the cost.
Does returning a JavaScript file from a REST API count as code on demand?
Only if the client actually executes that file. If the API returns a URL to a JavaScript file, that is just data. The constraint is about the server sending the code itself, not about referencing it. If the client fetches the file and runs it, then yes, that is code on demand.
Can code on demand be used with mobile apps?
It is possible, but rare. Mobile apps are typically compiled and deployed through an app store, so receiving executable code from a server is often against the platform’s rules. Some apps use JavaScript engines to run code dynamically, but that is not standard practice. In general, code on demand is most practical in web browsers.
What are the security risks of code on demand?
The main risk is that the client executes arbitrary code from the server. If the server is compromised, it can send malicious code to every client. Even if the server is trusted, the code could have bugs that cause security issues. This is why code on demand is often disabled in enterprise environments and why many APIs choose not to use it.
Related
Formula used
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.