Skip to contentSkip to navigationSkip to topbar
On 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

creating-main-keys page anchor

The required keys can be created in the Console(link takes you to an external page) by selecting Main as the Key Type.

New API Key.

Creating a new Subaccount when Public Key Client Validation is enforced

creating-a-new-subaccount-when-public-key-client-validation-is-enforced page anchor

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

setting-up-a-subaccount-via-api page anchor
  1. Create a new Subaccount with the key created above.
  2. Seed the new account with an API Key
  3. Seed the new account with a Public Key
  4. Request with new account credentials
1
import com.twilio.rest.accounts.v1.credential.PublicKey;
2
import com.twilio.rest.api.v2010.Account;
3
import com.twilio.rest.api.v2010.account.NewKey;
4
import com.twilio.http.TwilioRestClient;
5
import com.twilio.http.ValidationClient;
6
import java.security.PrivateKey;
7
8
public class NewSubAccount {
9
private static final String ACCOUNT_SID = CredStore.getEnv("TWILIO_ACCOUNT_SID");
10
private static final String API_KEY = CredStore.getEnv("TWILIO_MAIN_KEY");
11
private static final String API_SECRET = CredStore.getEnv("TWILIO_MAIN_SECRET");
12
private static final String PUBLIC_KEY_SID = CredStore.getEnv("TWILIO_PUBLIC_KEY_SID");
13
private static final PrivateKey PRIVATE_KEY = CredStore.getPrivateKey();
14
private static final String PUBLIC_KEY = CredStore.getPublicKey();
15
16
public static void main(String[] args) {
17
18
//Create client with Main Account Credentials
19
TwilioRestClient client = new TwilioRestClient.Builder(API_KEY, API_SECRET)
20
.accountSid(ACCOUNT_SID)
21
.httpClient(new ValidationClient(ACCOUNT_SID, PUBLIC_KEY_SID, API_KEY, PRIVATE_KEY))
22
.build();
23
24
//Create new Subaccount
25
Account myAccount = Account.creator().setFriendlyName("PKCV Account").create(client);
26
String myAccountSid = myAccount.getSid();
27
28
//Seed API Key
29
NewKey myKey = NewKey.creator(myAccountSid).setFriendlyName("PKCV Key").create(client);
30
31
//Seed Public Key
32
PublicKey myPubKey = PublicKey.creator(PUBLIC_KEY)
33
.setAccountSid(myAccountSid)
34
.setFriendlyName("Seed PK")
35
.create(client);
36
37
//Create a client for new Subaccount
38
TwilioRestClient newClient = new TwilioRestClient.Builder(myKey.getSid(), myKey.getSecret())
39
.accountSid(myAccountSid)
40
.httpClient(new ValidationClient(myAccountSid, myPubKey.getSid(), myKey.getSid(), PRIVATE_KEY))
41
.build();
42
43
//Make API call with new account and list public key sid(s) assigned to account
44
Iterable pks = PublicKey.reader().read(newClient);
45
for (PublicKey pk : pks) {
46
System.out.println("key: " + pk.getSid() + " - friendlyName: " + pk.getFriendlyName());
47
}
48
49
//Clean up
50
Account.updater(myAccountSid).setStatus(Account.Status.CLOSED).update(client);
51
}
52
}

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

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.