Skip to content

Instantly share code, notes, and snippets.

@Jamedjo
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jamedjo/aac9a3ec993e97c65f34 to your computer and use it in GitHub Desktop.
Save Jamedjo/aac9a3ec993e97c65f34 to your computer and use it in GitHub Desktop.
Script to create sendwithus customers from csv with data
def batch(iterable, n=1):
l = len(iterable)
for ndx in range(0, l, n):
yield iterable[ndx:min(ndx+n, l)]
import sendwithus
import csv
max_count = 100 # keep this as or around 100 for batch requests
csv_location = './customers.csv'
api_key = 'SWU_PRODUCTION_API_KEY'
data = {}
api = sendwithus.api(api_key=api_key) #
batch_request = api.start_batch()
users_list = []
with open(csv_location, 'rU') as csvfile:
users = csv.DictReader(csvfile)
for user in users:
email = user['email']
del user['email']
users_list.append((email, user))
print '%d customers in list' % len(users_list)
for index, user_batch in enumerate(batch(users_list, max_count)):
requests_list = []
for user_email, user_data in user_batch:
print '%s user with data %s' % (user_email, user_data)
batch_request.customer_create(user_email, data=user_data)
print 'Starting batch %s!' % index
print batch_request.execute()
email first_name last_name
jon@example.co.uk Jon Smith
test@example.com Test Er
james@example.com James Jones
@demoore
Copy link

demoore commented Jul 3, 2015

Line 11! 🐱
That needs to be a sendwithus production key, you'll find that in your API settings. (looks like live_2jklasdjflkj…)

Make sure to use a private gist if you include it ;)

@Jamedjo
Copy link
Author

Jamedjo commented Jul 3, 2015

Yep, thought it best to leave that out when sharing!

Thanks again for the original

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment