title: Ways of sending data between FastAPI router methods / endpoints
body: <p>Let's say we have two minimal Fast API endpoints:</p>
  <pre class="lang-py prettyprint-override"><code>from urllib.parse import urlencode
  
  import httpx
  from fastapi import APIRouter
  
  router = APIRouter()
  
  
  @router.post(&quot;/api/endpoint_1&quot;)
  async def endpoint_1(attributes):
      # process the attributes
      return attributes
  
  
  @router.get(&quot;/api/endpoint_2&quot;)
  async def endpoint_2():
      # here I need to receive the attributes from POST
      attributes = get_attributes_from_somewhere()
  
      url = f&quot;http://some_external_service_url.com/api/{urlencode(attributes, doseq=True)}&quot;
      async with httpx.AsyncClient() as client:
          response = await client.get(url)
          return response.json().get(&quot;items&quot;)
  </code></pre>
  <p>Now imagine some client does a <code>POST</code> request against <code>endpoint_1</code> with some <code>attributes</code>. I need to take these attributes and use them inside <code>endpoint_2</code> to make some further requests to some external services. The data returned by <code>endpoint_2</code> is then going to be retrieved by my frontend and rendered to the user.</p>
  <p>What's the best recommended way to send the data from <code>endpoint_1</code> to <code>endpoint_2</code> in Fast API?</p>
  <p>I've thought about the following:</p>
  <ul>
  <li><p>Save the data in some local DB in <code>endpoint_1</code> and retrieve it back in <code>endpoint_2</code>. After retrieving the data, delete the attributes from DB. This seems like having a lot of overhead for just keeping one table that's really ever going to have one record.</p>
  </li>
  <li><p>Save the data using some cache mechanism (Redis / Memcached) but then again, configuring this just to keep this seems also peculiar.</p>
  </li>
  </ul>
  <p>To have a bigger picture about this, I'm using Fast API + Python 3.9 on the backend to create a REST service, along with the <code>httpx</code> library to make async requests to external services. On the other side, for frontend I'm using React and Typescript. The frontend is only ever going to call <code>endpoint_2</code> to display the <code>items</code>. <code>endpoint_1</code> is only going to be called by some client service.</p>