Importing users with the Zendesk API

Have more questions? Submit a request

17 Comments

  • Ben Heiligenthal
    Comment actions Permalink

    I'm working on a script that will continually run to CREATE OR UPDATE MANY users. My script was working fine, but now we want to include all email addresses, not just the primary. I found the "identities" property, and was able to get it to work for a single users just using the CREATE OR UPDATE API. Now, when I try to add it to my MANY script I receive the error:

    Invoke-RestMethod : { "error": { "title": "Invalid attribute", "message": "You passed an invalid value for the identities attribute. Invalid parameter: identities must be a hash" } }

     

    I'm confused because I used identities as an array with a single user and it worked, but now it seems to expects a different format for the MANY call. Can someone give me the format for how to use identities in a CREATE OR UPDATE MANY API request?

     

    0
  • Christopher Cooper
    Comment actions Permalink

    Hi Ben,

    I was able to successfully use our Create Or Update Many Users API while including multiple email identities for the users. In my testing, the first identity in the array becomes the primary identity. Here is an example of the payload I used:

    curl https://{subdomain}.zendesk.com/api/v2/users/create_or_update_many.json \
    -d '{"users": [{"name": "user1", "identities": [{ "type": "email", "value": "user1_email1@gmail.com"}, {"type": "email", "value": "user1_email2@gmail.com" }]}, {"name": "user2", "identities": [{ "type": "email", "value": "user2_email1@gmail.com"}, {"type": "email", "value": "user2_email2@gmail.com" }]}]}' \
    -H "Content-Type: application/json" -X POST \
    -v -u {email_address}:{password}

    I hope this helps. Please let me know if you need additional assistance with this.

    0
  • Ben Heiligenthal
    Comment actions Permalink

    Thanks Christopher,

    I realized my mistake, when converting to JSON it had a default depth of 3, so the identities was getting cut off. After I set the Depth to 5 things worked!

    Thanks for the help

    0
  • Gary Kester
    Comment actions Permalink

    Hi Charles,

    Any chance you have a version of this that would work with a csv?

    Regards,

    Gary

    0
  • Charles Nadeau
    Comment actions Permalink

    I don't have a dedicated article but you could easily modify the code in this one to work with csv files. Python comes with it's own native csv library so you don't need to install anything else.

    The following possible rewrite of the code in the article is very back-of-the-envelope and not tested but it should point you in the right direction.

    import csv
    
    users_dict = {'users': []}
    
    users_file = 'users.csv'
    with users_file as f:
        reader = csv.reader(f)
        for row in reader:
            if row[2]:
                users_dict['users'].append(
                    {
                        'name': row[2],
                        'email': row[3],
                        'user_fields': {'member_level': row[7]}
                    }
                )

    More info on the csv module at  https://docs.python.org/3/library/csv.html.

     

    0
  • Yann Hervé-Bazin
    Comment actions Permalink

    Hi,

    Is it normal to receive a 403 error when running this "API POST /api/v2/users/create_many.json" on a Trial Account ?

    0
  • Bryan - Community Manager
    Comment actions Permalink

    Yes Yann Hervé-Bazin -- this API is disabled on trial accounts.

    0
  • Maria Skilbred
    Comment actions Permalink

    Great article and great questions! Can either the bulk import or the API import be scheduled to automatically upload daily files? If it does, would you please share with me how to configure it in Zendesk?

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Maria,

    You could use a utility that runs a process on your system at a scheduled time to upload the files daily. This is also referred to as a "chron job". In Python, you could use the APScheduler library to do this. Without going into the specifics, the code would look something like this:

    # run scheduled job
    scheduler = BackgroundScheduler(daemon=True)
    scheduler.add_job(import_users, 'interval', hours=24)
    scheduler.start()
    0
  • Maria Skilbred
    Comment actions Permalink

    We are getting error 401. I reviewed all the questions and answers, and I did not see anyone reporting this error code. Is it related to authentication? We are not using a token, just:  session.auth = 'your_zd_email', 'your_zd_password' - please advise. Thank you!

    0
  • Unibet
    Comment actions Permalink

    Looks like an error with how you setup authorization 

    This article might help

    https://develop.zendesk.com/hc/en-us/community/posts/360004389168-Zendesk-API-throwing-401-Unauthorized-error

    0
  • Maria Skilbred
    Comment actions Permalink

    Thank you! That worked! It sounds like Zendesk may have made some changes to the use of the password when connecting to the API. The use of the token resolved that issue.

    0
  • Unibet
    Comment actions Permalink

    Great. There are different strategies in using tokens vs passwords but I dont think displaying an admin password sits well with me especially if you intend having someone else maintain your code

    0
  • Maria Skilbred
    Comment actions Permalink

    I agree, thanks, Gary! By any chance, do you happen to know if there is a notification that can be set up in Zendesk after uploading the file? Even though the Python script is coded to log a success/error message, it would be nice to also get a notification from Zendesk. 

    0
  • Unibet
    Comment actions Permalink

    The server response already confirms a successful upload and you can start searching for any clients within a minute from experience.

    I've created a log file and update it with the server response to confirm when I last did a successful import with the following bit of code:

    I import the time module
     
    create a time stamp
    time_stamp = time.asctime()
     
    Then add the server response and time stamp to the log:
     
        #adds an entry to import log in csv saved to zendesk_import_log
        with open(zendesk_import_log, "a", newline='') as csvfile:
            log_write_row = csv.writer(csvfile)
            log_write_row.writerow([server_response, time_stamp])
     
    0
  • Gary Kester
    Comment actions Permalink

    If I create / update 2000 users, does this take some time to index before I can search against it?

    0
  • Charles Nadeau
    Comment actions Permalink

    Hi Gary,

    The API rate limit varies depends on your plan level and other factors. See Usage limits in the reference docs.

    Because the Create Many Users endpoint creates a job to create records instead of creating the records directly, there will be a delay before you can search for them. You can use the Job Status API to monitor the status of the job. Confirm that the status of the job returns "completed" before searching for the users.

    Thanks.

     

    0

Please sign in to leave a comment.

Powered by Zendesk