# Auto Create User from Ticket-API

**URL:** <https://community.zammad.org/t/auto-create-user-from-ticket-api/6850>\
**Category:** Feature requests\
**Created:** [May 7, 2021, 7:48am UTC](https://community.zammad.org/t/auto-create-user-from-ticket-api/6850 "2021-05-07T07:48:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZuSe](https://community.zammad.org/user_avatar/community.zammad.org/zuse/32/4193_2.png) [@ZuSe](https://community.zammad.org/u/ZuSe)\
**Post date:** [May 7, 2021, 7:48am UTC](https://community.zammad.org/t/auto-create-user-from-ticket-api/6850/1 "2021-05-07T07:48:05Z")

</div>

Hi guys,

following this ticket:

> [@Auto-Create User when creating a ticket with the API and using request\_on\_behalf\_of](https://community.zammad.org/t/auto-create-user-when-creating-a-ticket-with-the-api-and-using-request-on-behalf-of/3353):
>
> Hi, I’m not really sure whether this is a feature request or would fit better in technical assistance: I’d like a Application create Tickets on behalf of a user that might or might not exist within zammad at that moment. For having the user auto-created within ticket creation there is the guess:user@e.mail way, but that does not seem to work for the user field in the X-On-Behalf-Of header (resulting in Unauthorized as reaction). I use the header because I want everything to work as if the user…

I did exactly like bernd and try to create a ticket with an unknown user using the guess: way  
Unfortunately it doesnt work.

Are there any changes with zammad 4.x that caused it to stop working?  
If so, wouldnt make it support it again. Especially when providing Supports-Widget on web I love to not check/create for existing users but let the system do it right away.

Here is my request:

```
    request_body = {
        "title": f"Form Submission - {form_name}",
        "group": "Users",
        "customer_id": f"guess:{email}",
        "note": "Note",
        "article": {
            "subject": "Anfrage {form_name}",
            "body": f"{request.data}",
            "from": name,
            "to": email,
            "type": "email",
            "internal": False
        }
    }

    headers = {
        "Authorization": f"Token {settings.ZAMMAD_TOKEN}"
    }

    response = requests.post(url=f'{settings.ZAMMAD_URL}/api/v1/tickets',
                  json=request_body,
                  headers=headers
                  )

```

I tried with both X-Behalf-of header and without. The ticket is created is created when using x-behalf-of, but then with the existing account instead of the new one that should be created.

---

<div class="post-metadata">

**Author:** ![synesty](https://community.zammad.org/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@synesty](https://community.zammad.org/u/synesty)\
**Post date:** [May 7, 2021, 9:32am UTC](https://community.zammad.org/t/auto-create-user-from-ticket-api/6850/2 "2021-05-07T09:32:23Z")

</div>

Oh yes, please.  
We have an own “Create new ticket” form in our application and are jumping through the following hoops to create a ticket:

```
Check if user exists 
GET /api/v1/users/search?limit=1&query=email.keyword:potentiallyNewCustomer@example.com X-On-Behalf-Of null

if not exist, create it
POST /api/v1/users X-On-Behalf-Of null

create ticket with the user.id of the new or existing user from above:
POST /api/v1/tickets/ with X-On-Behalf-Of: user.id from above

```

Would be great to just do it in one shot.  
The newly created ticket should belong to the customer (by email).

Initially I would have had expected that `X-On-Behalf-Of` would work like this, but it fails if the user does not exist yet.

---

<div class="post-metadata">

**Author:** ![Zwieback](https://community.zammad.org/user_avatar/community.zammad.org/zwieback/32/4419_2.png) [@Zwieback](https://community.zammad.org/u/Zwieback)\
**Post date:** [June 28, 2021, 8:52am UTC](https://community.zammad.org/t/auto-create-user-from-ticket-api/6850/3 "2021-06-28T08:52:26Z")

</div>

You can simplify this by just

POST `/api/v1/users` with the `potentiallyNewCustomer@example.com`

and

POST `/api/v1/tickets/` with X-On-Behalf-Of `potentiallyNewCustomer@example.com`

This will create a user if non exists and returns an error when a users already exists.  
Then just create the ticket with the e-mail

So you can save yourself the first call.

---

<div class="post-metadata">

**Author:** ![synesty](https://community.zammad.org/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@synesty](https://community.zammad.org/u/synesty)\
**Post date:** [June 28, 2021, 1:16pm UTC](https://community.zammad.org/t/auto-create-user-from-ticket-api/6850/4 "2021-06-28T13:16:38Z")

</div>

Ok thanks. This saves the first call.  
I need to find out how the error looks like. Unfortunately errors are often an HTML page instead of a JSON response. Maybe we decided for the approach with 3 calls, so we have proper JSON everytime, as HTML-error pages also could mean “Service unavailable”, and parsing error message in HTML is not fun 😛

---

<div class="post-metadata">

**Author:** ![system](https://community.zammad.org/uploads/default/original/1X/5c5afaea2d23f811dbec33cd4edb02fa29d3262f.png) [@system](https://community.zammad.org/u/system)\
**Post date:** [June 27, 2022, 11:48pm UTC](https://community.zammad.org/t/auto-create-user-from-ticket-api/6850/5 "2022-06-27T23:48:43Z")

</div>

This topic was automatically closed after 416 days. New replies are no longer allowed.
