The Web API Framework in DataFlex
Lesson 4 - Routers
In this quick lesson, we’re diving into routers — a small but powerful piece of your web API puzzle. Ready? Let’s go!
- Step 1: Understand What Routers Are
Routers themselves are just a way to organize your URL paths. By default, they don’t do much alone — just a neat way to group endpoints under one path. But combine routers with things like authentication or authorization, and you unlock powerful features. We’ll get there soon! - Step 2: Create Your Router
Let’s jump into the code. Going back to our cWebApi object, where you already have your endpoints set up. Now, create a router:- oMyRouter is a cWebApiRouter
End_Object
- Step 3: Set the Router’s Path
Next, assign a path to your router. Let’s use the DF Learning Center as our example path:- Object oMyRouter is a cWebApiRouter
Set psPath to “dflc”
End_Object
- This means any endpoint inside this router will start with /dflc in the URL.
- Step 4: Move an Endpoint Inside Your Router
Now, take one of your existing endpoints and move it inside the router. For example, move the inventory endpoint inside myRouter like this: - Step 5: Compile and Test
Compile your project and open your browser to check the OpenAPI specification. You should now see your inventory endpoint listed under the /dflc URL path — something like: - Step 6: What’s Next?
Right now, the router itself just changes your URL structure — handy for organizing your API, but not yet game-changing. In the next lesson, we’ll add authentication to this router, making it super powerful by controlling access and security.
You’re all set to experiment with routers! Want to keep leveling up? Stay tuned for the authentication lesson next.