This article is about PyScript and the MongoDB Data API. I have not used the MongoDB Data API before. I know MongoDB and PyScript fairly well, so I thought I would document my journey to see if they will work together. My only real concern is CORS. The HTTP REST portion should be easy to work with.


  • Basic understanding of Python and PyScript
  • Basic understanding of MongoDB
  • MongoDB Atlas account. Sign up link.


I am using the MongoDB sample dataset. The MongoDB Atlas GUI has an option on the Database Deployments page “Load Sample Dataset”. Load Sample Data

Enable the MongoDB Data API

Once you have your MongoDB Atlas account configured, enable the Data API. In the MongoDB Web GUI, click on Data API in the left column.

Select the data source(s) and click Enable the Data API.

Once the Data API is enabled, save the URL Endpoint.

For each Data Source, select the Data API Access permissions, such as Read Only or Read and Write. For my work, I selected Read and Write.

You will need an API Key. Click the Create API Key button. Name your key and click the Generate API Key button.

The panel will display examples in cURL, Python, etc. Copy the cURL and Python examples.

For the examples, I selected my cluster for the Linked Data Source, sample_mflix for the Database and movies for the Collection.

Testing with the cURL example

Since I use Windows for my development workstation, I must modify the cURL example.

Original example:

On Windows, it is much easier to move cURL data to a file. I first replaced the --data-raw argument with -d @test.json:

The modified example is:

Executing that command results in:

Excellent, the API works from my desktop using non-browser tools.

MongoDB Data API and CORS

My big question now is does the MongoDB API support CORS so that browsers can make requests?

I wrote a simple PyScript Python program. Update with the correct URL and API Key.

Sadly to say, MongoDB did not think of web browsers when designing the MongoDB Data API. My code receives the following error:

What is the solution?

There are two solutions:

  • MongoDB can improve the Data API to support specifying CORS header in the Data API configuration.
  • Use a proxy application that runs alongside the browser to add CORS headers.

Since I have no control over the Data API, I decided to write a CORS proxy for PyScript. I will write another article once I am ready to publish.

MongoDB Data API and CORS

MongoDB has a page regarding the DATA API and CORS. Please click this link and vote so that they know how important this is:

What should MongoDB provide as a solution?

The MongoDB Data API should:

  • Provide a method of specifying allowed origins and HTTP methods.
  • Respond to the HTTP OPTIONS method with an HTTP Status Code 200 and the following headers as a minimum:
    • access-control-allow-headers
    • access-control-allow-methods
    • access-control-allow-origin
    • access-control-allow-credentials

I wrote a Python program to send an OPTIONS request to the DATA API. MongoDB does return CORS headers. If I send the CORS header

headers["Access-Control-Request-Headers"] = "api-key,content-type"

then the response does not have CORS headers. The item that makes a difference is api-key.

For PyScript, I discovered that if I remove the HTTP header api-key the request passes the CORS preflight test and the response is HTTP Status Code 400:

I think this means MongoDB is close to enabling CORS support.

More Information

Photography Credit

I write free articles about technology. Recently, I learned about which provides free images. The image in this article is courtesy of Pixabay at Pexels.