# Article: Context.APIException - User context must first be passed to setUserContext

**URL:** <https://talk.openmrs.org/t/article-context-apiexception-user-context-must-first-be-passed-to-setusercontext/2837>\
**Category:** Development\
**Tags:** api, error\
**Created:** [August 21, 2015, 3:15pm UTC](https://talk.openmrs.org/t/article-context-apiexception-user-context-must-first-be-passed-to-setusercontext/2837 "2015-08-21T15:15:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ch3ck](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ch3ck/32/3177_2.png) [@ch3ck](https://talk.openmrs.org/u/ch3ck)\
**Post date:** [August 21, 2015, 3:15pm UTC](https://talk.openmrs.org/t/article-context-apiexception-user-context-must-first-be-passed-to-setusercontext/2837/1 "2015-08-21T15:15:12Z")

</div>

Hello Everyone,

I have come to realise that there is a popular error developers have been facing while hacking OpenMRS which is:

> org.openmrs.api.Context.APIException: A user context must first be passed to setUserContext()…use Context.openSession() and (closeSession() to prevent memory leaks!) before using the API."

I want to write a Blog Article on this problem ie: possible causes and solutions to each case. @michael suggested I could use gist on github for this. I am currently exploring the threads on this error and any suggestions from the Developer community and possible causes and solutions will be greatly appreciated. I’ll publish the link to the Blog post to the community once I’ve taken everyone’s suggestions.

Cheers! Nyah

---

<div class="post-metadata">

**Author:** ![teleivo](https://talk.openmrs.org/user_avatar/talk.openmrs.org/teleivo/32/2015_2.png) [@teleivo](https://talk.openmrs.org/u/teleivo)\
**Post date:** [April 18, 2016, 3:26pm UTC](https://talk.openmrs.org/t/article-context-apiexception-user-context-must-first-be-passed-to-setusercontext/2837/2 "2016-04-18T15:26:08Z")

</div>

Dear @ch3ck

I am facing this issue now. I am tying to use spring componentscan in the radiology modules moduleApplicationContext. It instantiates a bean of a class annotated with spring’s @component. The error occurs when I call a method on a member that is autowired. Some member of this member seems to need the OpenMRS api context.

Do you have any suggestions?

---

<div class="post-metadata">

**Author:** ![ch3ck](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ch3ck/32/3177_2.png) [@ch3ck](https://talk.openmrs.org/u/ch3ck)\
**Post date:** [April 19, 2016, 7:20am UTC](https://talk.openmrs.org/t/article-context-apiexception-user-context-must-first-be-passed-to-setusercontext/2837/3 "2016-04-19T07:20:37Z")

</div>

Hello @teleivo

This problem is very common and usually depends on the context. I think this thread discusses most of it: [Context API Exception issue](https://issues.openmrs.org/browse/JRPT-4). This is the best reference I have seen on this error; hope it helps.

Cheers! Nyah

---

<div class="post-metadata">

**Author:** ![mdg583](https://talk.openmrs.org/letter_avatar/mdg583/32/5_5575768a8748004e209b776fc1b2916d.png) [@mdg583](https://talk.openmrs.org/u/mdg583)\
**Post date:** [June 14, 2017, 7:27am UTC](https://talk.openmrs.org/t/article-context-apiexception-user-context-must-first-be-passed-to-setusercontext/2837/4 "2017-06-14T07:27:24Z")

</div>

Hello,

I also see this error on my openmrs test system for any bad url, ie going to [http://localhost:8080/openmrs/thisisabadurl](http://localhost:8080/openmrs/thisisabadurl)

Maybe this is an issue with my test deployment?
