Contentful logo

Contentful Community

How to create entries that contain media

I’m trying to create entries in python, and map my data to the content model I have created.
My product entry contains a media field, that accepts one more more images. How do I create entries that add these images? Are images assets? Do I create these assets first, and then pass on their IDs to the entry somehow?

This is the straight forward method I have tried, that do not work:

# auth + fetched the space object ... then:
entry_attributes = {
    'content_type_id': 'product',
    'fields': {
        'product_name': {
            'nb-NO': 'Nice product'
        'description': {
            'nb-NO': 'The best product that money cannot buy.'
    ... more normal fields, then ...
    'images': {
        'items': {
            'nb-NO': '',
            'nb-NO': ''
# and create entry from this dict:
new_entry_id = None # Use `None` if you want the API to autogenerate the ID
new_entry = space.entries().create(
print("Did I create a thing now?")

This does not work. Although the JSON preview of the content model looks to me like it could work.
Also: just pasing the URLs to the images as a list (the media field is of type Array, according to the same JSON preview) does not work:
JSON preview:

      "id": "images",
      "name": "images",
      "type": "Array",
      "localized": false,
      "required": false,
      "validations": [],
      "disabled": false,
      "omitted": false,
      "items": {
        "type": "Link",
        "validations": [],
        "linkType": "Asset"

alternative dict struture for list

   'images': {
        'items': [ '', '']

How should this be done properly? How do I pass along images with a new entry?

1 Like

Hi @eirik.stavelin,

The issue here is that you should first create your images as assets (as well as process and publish them), and then reference these inside your entries.

Images are taken as assets and you should separately create them as so, before referencing them in given entries.

Also, when creating the asset, you should pass the entire required JSON payload in your request, instead of just the image URL itself, as described in the following section of our documentation:

After doing that, you would just reference the image through the asset ID in the media field of your entries, as described in the following examples:

Let me know if that helps out or if you have any other questions :slightly_smiling_face:

Thanks @gabriel
That made sense, and the links where great. I was quickly prototyping while running into this, and ended up skipping this images as assets (just stored their urls as text to be fast). The thing that was troublesome was that I wanted to import a small set of data, and kept running into errors, I think what was the problem is this:
In order to be able to say asset.publish() you need to have done asset.process() - but if you do publish before process() is done, errors happen. As I did not find any way to test for an assets process-status (like the asset.is_published test), I found errors too often for this to be fast for a small dataset consistently (without manual wrangling).
I’ll get back to this in the next iteration, and post my code as example when I get it right.

Hi @eirik.stavelin,

There is no testing mechanism or web-hook event available to check the actual processing of assets.

In general, a small delay between sending a asset.process() and asset.publish() request should be enough to guarantee that your asset is published without any issues. On top of that, you could also add a retry mechanism to your workflow and that should cover for the errors that could eventually happen.