This is the python sdk for Splitwise APIs. Pull requests and bug reports are welcomed.
The latest version of splitwise SDK is Splitwise-3.0.0
The detailed docs are hosted at readthedocs.org
Install using pip :
$ pip install splitwise
Register your application on splitwise and get your consumer key and consumer secret.
from splitwise import Splitwise
To get an instance to splitwise just provide the consumer key and secret.
sObj = Splitwise("<consumer key>","<consumer secret>")
To get the debug logs use
import logging
logging.basicConfig(level=logging.DEBUG)
Before you can make call to splitwise, you need to get access token of the user on whose behalf you will be making call. Think of it as login with splitwise. Its based on OAuth and its a 2 step process.
Get the Authorize URL and Secret. Redirect the user to the Authorize url and store the secret in somewhere for eg in session.
sObj = Splitwise("<consumer key>","<consumer secret>")
url, secret = sObj.getAuthorizeURL()
#Store secret so you can retrieve it later
#redirect user to url
After authorization splitwise will redirect the user back to the callback url that you provided during registration. It will also provide oauth_token and oauth_verifier that can be used along with secret from step 1 to get access token. The below snippet is from Flask application to extract parameters and then using SDK to get access token. This access token can be stored in your db to make calls on his/her behalf.
oauth_token = request.args.get('oauth_token')
oauth_verifier = request.args.get('oauth_verifier')
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
access_token = sObj.getAccessToken(oauth_token,session['secret'],oauth_verifier)
session['access_token'] = access_token
It is now possible to use OAuth2 with Splitwise. For details you can refer to readthedocs.org
You can use API Key provided by Splitwise to test APIs for your user.
sObj = Splitwise("<consumer key>","<consumer secret>",api_key="<api key>")
current = sObj.getCurrentUser()
Once you have the access token you can make the calls to splitwise. Get the splitwise instance and set the access token and then make authorized calls.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getFriends()
You can use getCurrentUser()
to get the current user. It returns a CurrentUser
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getCurrentUser()
You can use getUser(id)
to get the user. It returns a User
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
id = 7123
user = sObj.getUser(id)
You can use updateUser(user)
to update the user. It takes in a partial CurrentUser
object
with atleast id
set. It returns a CurrentUser
object.
Note that you can update anything for your user and first_name
, last_name
and email
for
any acquaintances who has not created account yet.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
user = User()
user.setId(10)
user.setFirstName("naman")
updated_user, error = sObj.updateUser(user)
print(updated_user.getFirstName())
You can use getFriends()
to get all the friends of the current user along with the balances. It returns a list of Friend
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getFriends()
You can use getGroups()
to get all the groups of the current user along with the members and balances. It returns a list of Group
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getGroups()
You can use getCurrencies()
to get all the currencies supported by splitwise. It returns a list of Currency
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getCurrencies()
You can use getCategories()
to get all the categories and sub categories provided by splitwise. It returns a list of Category
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getCategories()
You can use getGroup(id)
to get the particular group of the current user along with the members and balances. It returns a Group
object. Use id as 0 to get all non group expenses.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getGroup(43233)
You can use getExpenses(offset,limit,group_id,friend_id,dated_after,dated_before,updated_after,updated_before,visible)
to get all the expenses of the current user based on filter options. It returns a list of Expense
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getExpenses()
You can use getExpense(id)
to get the particular expense of the current user. It returns a Expense
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getExpense(43233)
You can use createExpense(Expense)
to create a new Expense. It takes in parameter a partial Expense
object and returns an Expense
object.
Following things need to be set on the Expense
object.
ExpenseUser
with id and paidShare and owedShare set.from splitwise.expense import Expense
from splitwise.user import ExpenseUser
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense = Expense()
expense.setCost('10')
expense.setDescription("Testing")
expense.setReceipt("/Users/naman/receipt.jpg")
user1 = ExpenseUser()
user1.setId(79774)
user1.setPaidShare('10.00')
user1.setOwedShare('2.0')
user2 = ExpenseUser()
user2.setId(281236)
user2.setPaidShare('0.00')
user2.setOwedShare('8.00')
users = []
users.append(user1)
users.append(user2)
expense.setUsers(users)
expense, errors = sObj.createExpense(expense)
print expense.getId()
You can use createGroup(Group)
to create a new Group. It takes in parameter a partial Group
object and returns an Group
object.
Following things need to be set on the Group
object.
User
with either FirstName, LastName and Email or just Id set.from splitwise.group import Group
from splitwise.user import User
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
group = Group()
group.setName("Testing")
user1 = User()
user1.setId(79774)
user2 = User()
user2.setId(281236)
users = []
users.append(user1)
users.append(user2)
group.setMembers(users)
group, errors = sObj.createGroup(group)
print group.getId()
You can use addUserToGroup(User, group_id)
to add user to group. It takes in a splitwise.user.User
object that has either id
or firstName
and email
set and a group_id
.
from splitwise.group import Group
from splitwise.user import User
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
user = User()
user.setId(1223)
success, user, errors = sObj.addUserToGroup(user, 4456)
print(success)
You can use deleteGroup(group_id)
to delete an existing group.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
success, errors = sObj.deleteGroup(4456)
print(success)
You can use updateExpense(Expense)
to update an existing Expense. It takes in parameter a partial Expense
object and returns an Expense
object.
Following things need to be set on the Expense
object.
from splitwise.expense import Expense
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense = Expense()
expense.id = 12345678
expense.setCost('10')
expense.setDescription("Updated description")
expense, errors = sObj.updateExpense(expense)
print(expense.getId())
or update the fetched expense
from splitwise.expense import Expense
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense, error = sObj.getExpense(12345)
expense.setDescription("Updated description")
expense, errors = sObj.updateExpense(expense)
print(expense.getDescription())
You can use deleteExpense(expense_id)
to delete an existing expense.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
success, errors = sObj.deleteExpense(4456)
print(success)
You can use getComments(id)
to get the comments made on an expense. It returns an array of Comment
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
id = 982430660
comments = sObj.getComments(id)
You can use createComment(Comment)
to create a new Comment. It takes in parameters expense_id and content and returns a Comment
object.
Following are the parameters passed.
from splitwise import Splitwise
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense_id = 982430660
content = "Test for create comment"
comment, errors = sObj.createComment(expense_id,content)
print("content:", comment.getContent())
print("errors:", errors)
You can use getNotifications()
to get recent Notifications. It returns an array of Notification
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
id = 982430660
notifications = sObj.getNotifications()
Methods:
Picture
object containing picture detailsCurrent user is inherited from User
. All the methods of user are available for current user.
Methods:
Friend is inherited from User
. All the methods of user are available for friend.
Methods:
Balance
objectsFriendGroup
objectsExpenseUser is inherited from User
. All the methods of user are available for Expense User.
Methods:
Methods:
Friend
objectsDebt
objectsDebt
objectsFriend
objectsFriend
objectsMethods:
Balance
objectMethods:
Methods:
Category
objectsMethods:
Methods:
Methods:
User
object of user who created the expenseReceipt
object for receiptCategory
object for categoryUser
object of user who last updated the expenseUser
object of user who deleted the expenseExpenseUser
objectsDebt
objectsReceipt
object for receiptCategory
object for categoryExpenseUser
objectsExpenseUser
objectsMethods:
Methods:
Methods:
User
object containing user detailsMethods:
Used with Notifications.
Methods:
This is the GitHub Link to the sample application written in Flask to show the usage of splitwise application.
MIT
If you think this helped you and you want to donate, you can do it via -
https://www.paypal.me/namanagg
1DNhyZ696ekq6sY5vYMcmBLxLzAtq3oYpM