How do we create an API Mode Context?


I’m trying to create a context that is in API Mode

Steps I’ve taken so far…

  1. Created a context with key: api
  2. Created a System Setting field type: context, key: MODX_API_MODE, value: true
  3. Created a Context Setting type: text, key: MODX_API_MODE, value: true

Observed behavior

Accessing a resource within the api context loads the web context.

Expected behavior

Accessing a resource in the api context loads the api context.


MODX 2.8.1-pl

You’ll need something to route contexts, typically an extra like XRouting which routes based off http_host, base_url, and site_url context settings. At that point you’ll be able to access resources in your second context.

MODX_API_MODE does nothing.

If you can be more specific about what you’d like to do, perhaps we can help guide you in the right direction.

1 Like

Hi, Mark. There used to be an API mode, but I see in the docs maybe it’s been removed.

I’m trying to have an API mode context for doing API only operations through ajax calls. If I hit a script on the web context I get HTML encapsulated responses that are not compatible with the JS parser. It’s expecting raw JSON responses.

Maybe I need to downgrade MODX to a previous version that supports API mode?

I don’t quite understand how routing has anything to do with putting an entire context in API mode. I was previously able to simply specify that I wanted MODX_API_MODE to be true, then the MODX engine would only return raw responses without HTML encapsulations.

Did that completely change?

There’s MODX_API_MODE in the deprecated example here:

You don’t need that anymore, you can load the MODX object as per the first example.

The routing would be so you can use a context other than the default one.

Right, so I pasted that code into a snippet and I get an HTML encapsulated response, because the context loaded is web…so I fail to understand how it’s useful unless you create a folder and file outside of MODX.

The point of contexts is to switch the operating mode, right? So, do I switch the operating mode to API mode on the fly at the context load event? That’s how it should be done from a proper software engineering perspective.

That snippet lets you specify which context you load.


Contexts can represent different websites, or variations of websites such as different languages.

You’re talking about HTML encapsulation, so I assume you’re attempting to get the output of a particular resource? If so, you can change the way it’s served via the resource content_type field.

Ahh, the example shows web context to I assumed that you had to pass it that for it to function. I’ll try it with the other context key I created.

Thank you for the eye opener. it’s been a long day.

1 Like