Rate this page:

Account and Key Management with API Keys

To manage API Keys and Accounts via the API after enforcing Public Key Client Validation, a Main API Key is required. Once Public Key Client Validation is enforced, requests with Auth Tokens will not be successful anymore and by default, API Keys are not permitted to manage Accounts or Keys.

Creating Main Keys

The required keys can be created in the Console by selecting Main as the Key Type.

New API Key

Creating a new Subaccount when Public Key Client Validation is enforced

To create a new Subaccount and make a successful API request, the newly created account needs to be primed with its own API Key and Public Key. Only Main API Keys have the permissions to execute the required steps below.

Setting up a Subaccount via API

  1. Creating a new Subaccount with the key created above.
  2. Seed the new account with a API Key
  3. Seed the new account with a Public Key
  4. Make request with new account credentials

Sample Code

import com.twilio.http.TwilioRestClient;
import com.twilio.http.ValidationClient;

public class NewSubAccount {
    private static final String ACCOUNT_SID = CredStore.getEnv("TWILIO_ACCOUNT_SID");
    private static final String API_KEY = CredStore.getEnv("TWILIO_MAIN_KEY");
    private static final String API_SECRET = CredStore.getEnv("TWILIO_MAIN_SECRET");
    private static final String PUBLIC_KEY_SID = CredStore.getEnv("TWILIO_PUBLIC_KEY_SID");
    private static final PrivateKey PRIVATE_KEY = CredStore.getPrivateKey();
    private static final String PUBLIC_KEY = CredStore.getPublicKey();

    public static void main(String[] args) {

        //Create client with Main Account Credentials
        TwilioRestClient client = new TwilioRestClient.Builder(API_KEY, API_SECRET)
                .httpClient(new ValidationClient(ACCOUNT_SID, PUBLIC_KEY_SID, API_KEY, PRIVATE_KEY))

        //Create new Subaccount 
        Account myAccount = Account.creator().setFriendlyName("PKCV Account").create(client);
        String myAccountSid = myAccount.getSid();

        //Seed API Key
        NewKey myKey = NewKey.creator(myAccountSid).setFriendlyName("PKCV Key").create(client);

        //Seed Public Key
        PublicKey myPubKey = PublicKey.creator(PUBLIC_KEY)
                .setFriendlyName("Seed PK")

        //Create client for new Subaccount
        TwilioRestClient newClient = new TwilioRestClient.Builder(myKey.getSid(), myKey.getSecret())
                .httpClient(new ValidationClient(myAccountSid, myPubKey.getSid(), myKey.getSid(), PRIVATE_KEY))

        //Make API call with new account and list public key sid(s) assigned to account
        Iterable pks = PublicKey.reader().read(newClient);
        for (PublicKey pk : pks) {
            System.out.println("key: " + pk.getSid() + "  - friendlyName: " + pk.getFriendlyName());

        //Clean up

The Console also supports creating API Keys and adding Public Keys for new Subaccounts.

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.


        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. Please select the reason(s) for your feedback or provide additional information about how we can improve:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more