rdegges/django-twilio

Subaccounts and Credentials

Closed this issue · 13 comments

We use subaccounts to keep track of each client. These subaccounts each have their own SID and TOKEN. How can I use this package with individual credentials? Seems like the settings only account for one set of creds?

jheld commented

@KrunchMuffin I haven't used it, but the project does have a Credential model which may solve your use case.

Yes, I see that now

class Credential(models.Model):

Being a newbie tho, not sure how to implement it. Assuming it's not documented since I can't find anything in the docs?

jheld commented

In order to setup the twilio Client object (send a text, etc), you can use the discover_twilio_credentials function and pass in the current user object to it, to get the specific set of credential info.

When receiving a text/call, you may have to inspect the incoming request object to see which account is being used, otherwise I'm unsure how you would know.

I can provide examples of this if you need some help setting it up.

Sure, if you could provide an example, would be most appreciated. We don't do incoming, at least right now, but be good for others that come across this to know how to do it as well.

Thanks! 👍

jheld commented

Yes.

To access a specific Credential'd twilio Client:

from django_twilio.utils import discover_twilio_credentials
from django_twilio.client import Client
user = User.objects.get(id=my_user_id)
credentials = discover_twilio_credentials(user=user)
client = Client(*credentials)

I'll work on the other portion in the next week. I don't have a running server right now, but at least the first part is here.

I didn't see anything in the repo for generating credentials. Do you have to manually create a Credential entry or is there a helper function? I'd like to generate a subaccount for every user to make it easier to bill accounts for SMS usage.

jheld commented

Hi @steverecio I don't see anything currently in the code that programmatically allows you to generate a subaccount. The Credential model is supported on django admin, but that's about it.

Sounds like we may want to build in support for subaccount management. Although we are very behind on the twilio 6.x branch (medium priority to upgrade!), if we can support this via how the docs are written: https://www.twilio.com/docs/iam/api/subaccounts then we may be able to do so.

Since we have made a Credential model I agree, it would be nice for us to actually integrate it a bit more natively in our library. I may try to do it short term (maybe release 0.14.0).

I'm not sure if we would have a need to create a REST API to handle this flow; it's something we could consider down the line once we're happy with an easy-to-use internal helper.

jheld commented

I've implemented a basic function and might go with this, but if you have feedback let me know I'll incorporate and then create the PR:

def create_sub_account(user, twilio_client, friendly_name=None):
    new_account = twilio_client.api.accounts.create(friendly_name=friendly_name or user)
    new_credential = Credential.objects.create(user=user, account_sid=new_account.sid, auth_token=new_account.auth_token, name=new_account.friendly_name)
    return new_credential, new_account
jheld commented

@KrunchMuffin again I realize this is quite delayed from your initial question, but if you are still interested in this feature if you wouldn't mind reviewing/providing feedback here (or requesting I create an official PR!), please do so.

Given the example above is probably easy to use, I expect this could get merged within the short-term (within the month!). My project does not currently use sub-accounts, and yet, having a helper utility like this would make it easier, so I appreciate you creating this issue nonetheless.

Unfortunately that project was put on hold for now. But will likely revisit at some point.

@jheld I'd be happy to review the PR when its ready. I'd like to start using subaccounts in the near future so happy to help where needed.

jheld commented

@steverecio please see #174 for a (work in progress) branch. I haven't tested it at all -- I would probably write at least 1 test before going for merge.

jheld commented

this has been merged. please open new issues for any downstream problems.