# Mautic api to create batch contact not working

**URL:** <https://forum.mautic.org/t/mautic-api-to-create-batch-contact-not-working/30015>\
**Category:** Product Support\
**Created:** [November 22, 2023, 9:25am UTC](https://forum.mautic.org/t/mautic-api-to-create-batch-contact-not-working/30015 "2023-11-22T09:25:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knashash4215](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@knashash4215](https://forum.mautic.org/u/knashash4215)\
**Post date:** [November 22, 2023, 9:25am UTC](https://forum.mautic.org/t/mautic-api-to-create-batch-contact-not-working/30015/1 "2023-11-22T09:25:14Z")

</div>

Version - v3.2.4

I am trying to send data in batches from my database to mautic website to create contacts. Sometimes, the contact is being created on the website, but the other times, its not being created. Also, when I am trying to send the data in batches, only the one contact is being created. I was checking in the mautic logs, there is some error regarding the campaing-

[2023-11-22 14:25:02] mautic.ERROR: CAMPAIGN: Campaign Limit exceeded    
[2023-11-22 14:30:02] mautic.ERROR: CAMPAIGN: Campaign Limit exceeded    
[2023-11-22 14:41:02] mautic.ERROR: CAMPAIGN: Campaign Limit exceeded    
[2023-11-22 14:52:22] mautic.ERROR: CAMPAIGN: Campaign Limit exceeded

could this error be the reason, the contacts are not being created ?

---

<div class="post-metadata">

**Author:** ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)\
**Post date:** [November 24, 2023, 5:14pm UTC](https://forum.mautic.org/t/mautic-api-to-create-batch-contact-not-working/30015/2 "2023-11-24T17:14:00Z")

</div>

Hi there,

Could you please share what you’re using to create the contacts (eg the actual commands/code)? Without that we can’t really help you to troubleshoot.

---

<div class="post-metadata">

**Author:** ![knashash4215](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@knashash4215](https://forum.mautic.org/u/knashash4215)\
**Post date:** [November 25, 2023, 8:23am UTC](https://forum.mautic.org/t/mautic-api-to-create-batch-contact-not-working/30015/3 "2023-11-25T08:23:54Z")

</div>

> @periodic\_task(run\_every=datetime.timedelta(minutes=settings.UPDATE\_TO\_MAUTIC\_INTERVAL))  
> def update\_data\_to\_mautic() → str:  
> current\_time = datetime.datetime.now()  
> one\_hour\_before\_current\_time = current\_time - datetime.timedelta(minutes=settings.UPDATE\_TO\_MAUTIC\_INTERVAL)  
> user\_data = MauticBigTable.objects.filter(modified\_at\_\_gt=one\_hour\_before\_current\_time)  
> if user\_data:  
> create\_payload, edit\_payload = data\_for\_creation\_and\_edit(user\_data)  
> if create\_payload:  
> print(create\_payload)  
> response = requests.post(  
> url=MAUTIC\_BASE\_URL + “/contacts/batch/new”,  
> headers=MAUTIC\_HEADER,  
> data=json.dumps(create\_payload)  
> )  
> if “contacts” in response:  
> mautic\_ids = [contact[“id”] for contact in response[“contacts”]]  
> update\_mautic\_ids\_in\_mauticbigtable(create\_payload, mautic\_ids)  
> else:  
> return response.status\_code()
> 
> ```
> if edit_payload:
> batch_edit_contacts(edit_payload)
> 
> ```
> 
> def data\_for\_creation\_and\_edit(user\_data: Iterable[MauticBigTable]) → (List[Dict[str, None]], List[Dict[str, None]]):  
> create\_payload =   
> edit\_payload =   
> for data in user\_data:  
> payload = prepare\_payload\_to\_update\_to\_mautic(data)  
> if data.mautic\_id:  
> payload[“id”] = data.mautic\_id  
> edit\_payload.append(payload)  
> else:  
> create\_payload.append(payload)
> 
> ```
> print(create_payload)
> return create_payload, edit_payload
> 
> ```
> 
> def batch\_edit\_contacts(edit\_payload: List[Dict[str, None]]) → dict:  
> response = requests.patch(  
> url=MAUTIC\_BASE\_URL + “/contacts/batch/edit”,  
> headers=MAUTIC\_HEADER,  
> data=json.dumps(edit\_payload)  
> )  
> return response.json()
> 
> def prepare\_payload\_to\_update\_to\_mautic(instance: MauticBigTable) → Dict[str, None]:  
> payload = {  
> “email”: instance.email,  
> }  
> backend\_fields\_to\_update\_in\_mautic = BackendFieldMapping.objects.all()  
> for field\_to\_update in backend\_fields\_to\_update\_in\_mautic:  
> mautic\_field = field\_to\_update.mautic\_field\_id  
> mautic\_field\_datatype = field\_to\_update.mautic\_field\_data\_type  
> backend\_field = field\_to\_update.backend\_field  
> if mautic\_field and mautic\_field\_datatype:  
> value\_to\_store = getattr(instance, backend\_field)  
> if value\_to\_store:  
> if mautic\_field\_datatype == “multiselect”:  
> payload[mautic\_field] = value\_to\_store  
> else:  
> if isinstance(value\_to\_store, list):  
> payload[mautic\_field] = value\_to\_store  
> if mautic\_field\_datatype == “date”:  
> payload[mautic\_field] = str(value\_to\_store)  
> if mautic\_field\_datatype == ‘datetime’:  
> payload[mautic\_field] = str(value\_to\_store)  
> # payload[mautic\_field] = value\_to\_store  
> print(payload)  
> return payload
> 
> def update\_mautic\_ids\_in\_mauticbigtable(create\_payload: List[Dict[str, None]], mautic\_ids: List[int]):  
> for data, mautic\_id in zip(create\_payload, mautic\_ids):  
> email = data[“email”]  
> try:  
> contact = MauticBigTable.objects.get(email=email)  
> contact.mautic\_id = mautic\_id  
> contact.save()  
> except MauticBigTable.DoesNotExist:  
> pass

the above code is in the staging env, in my local it is working but its not working properly in the staging

---

<div class="post-metadata">

**Author:** ![knashash4215](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@knashash4215](https://forum.mautic.org/u/knashash4215)\
**Post date:** [December 1, 2023, 7:33am UTC](https://forum.mautic.org/t/mautic-api-to-create-batch-contact-not-working/30015/4 "2023-12-01T07:33:26Z")

</div>

can you please tell me what could be the issue?
