Skip to content

Commit

Permalink
chore: add documentation for orgs API and public oauth (#1056)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 authored Dec 11, 2024
1 parent ad54ea7 commit 86744d5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ After a brief delay, you will receive the text message on your phone.
> **Warning**
> It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out [How to Set Environment Variables](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) for more information.
## OAuth Feature for Twilio APIs
We are introducing Client Credentials Flow-based OAuth 2.0 authentication. This feature is currently in beta and its implementation is subject to change.

API examples [here](https://github.com/twilio/twilio-node/blob/main/examples/public_oauth.js)

Organisation API examples [here](https://github.com/twilio/twilio-node/blob/main/examples/orgs_api.js)

## Usage

Check out these [code examples](examples) in JavaScript and TypeScript to get up and running quickly.
Expand Down
37 changes: 37 additions & 0 deletions examples/orgs_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
var Twilio = require("../lib");

const clientId = process.env.ORGS_CLIENT_ID;
const clientSecret = process.env.ORGS_CLIENT_SECRET;
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const organizationSid = process.env.TWILIO_ORG_SID;

const orgsCredentialProvider = new Twilio.OrgsCredentialProviderBuilder()
.setClientId(clientId)
.setClientSecret(clientSecret)
.build();

const client = new Twilio();
client.setCredentialProvider(orgsCredentialProvider);
client.setAccountSid(accountSid);

client.previewIam
.organization(organizationSid)
.accounts.list()
.then((accounts) => {
console.log(accounts);
})
.catch((error) => {
console.log(error);
});

client.previewIam
.organization(organizationSid)
.accounts(accountSid)
.fetch()
.then((account) => {
console.log(account);
})
.catch((error) => {
console.log(error);
});
25 changes: 25 additions & 0 deletions examples/public_oauth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var Twilio = require("../lib");

const clientId = process.env.OAUTH_CLIENT_ID;
const clientSecret = process.env.OAUTH_CLIENT_SECRET;
const accountSid = process.env.TWILIO_ACCOUNT_SID;

const clientCredentialProvider = new Twilio.ClientCredentialProviderBuilder()
.setClientId(clientId)
.setClientSecret(clientSecret)
.build();

const client = new Twilio();
client.setCredentialProvider(clientCredentialProvider);
client.setAccountSid(accountSid);

const messageId = "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
client
.messages(messageId)
.fetch()
.then((message) => {
console.log(message);
})
.catch((error) => {
console.log(error);
});

0 comments on commit 86744d5

Please sign in to comment.