SDK for TypeScript and JavaScript Cloud Shell Quick Start
This quick start shows you how to quickly get started running sample code with the Oracle Cloud Infrastructure SDK for TypeScript and JavaScript using Cloud Shell.
This quick start shows you how to quickly get started running sample code with the Oracle Cloud Infrastructure SDK for TypeScript and JavaScript using Cloud Shell. The OCI SDK for TypeScript and JavaScript SDK is pre-installed globally through npm.
JavaScript Example
- Login to the Console.
- Click the Cloud Shell icon in the Console header. Note that Cloud Shell will execute commands against the region selected in the Console's Region selection menu when Cloud Shell was started.
- Create a file named
region_subscriptions_example.js
with the following example code, which lists the subscribed regions for your current tenancy:const identity = require("oci-sdk/node_modules/oci-identity"); const common = require("oci-sdk/node_modules/oci-common"); const provider = new common.ConfigFileAuthenticationDetailsProvider(); const compartmentId = provider.getTenantId() || ""; let identityClient; async function getSubscriptionRegions(tenancyId) { const regions = await identityClient.listRegionSubscriptions({ tenancyId: tenancyId }); return regions.items.map(region => { return region.regionName; }); } (async () => { identityClient = await new identity.IdentityClient({ authenticationDetailsProvider: provider }); const regions = await getSubscriptionRegions(compartmentId); console.log("Currently subscribed to the following region(s): ", regions) })();
- Create a file named
region_subscriptions_example.js
with the following example code, which lists the subscribed regions for your current tenancy:npm link oci-sdk
- Run the example:
node region_subscriptions_example.js
TypeScript Example
This TypeScript example will list the subscribed regions for the current tenancy.
- Create an empty project named ts_demo and link the global installation of
oci-sdk library using the following
commands:
# create a new project folder and move into it mkdir ts_demo cd ts_demo # initialize a new javascript/typescript project npm init --y # link the global installation of oci-sdk to the current project npm link oci-sdk npm link @types/node
- Create a file named
region_subscriptions_example.ts
inside your ts_demo project with the following code:import * as identity from "oci-sdk/node_modules/oci-identity"; import common = require("oci-sdk/node_modules/oci-common"); const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider(); const compartmentId = provider.getTenantId(); let identityClient: identity.IdentityClient; export async function getSubscriptionRegions(tenancyId: string) { const listRegionSubscriptionsRequest: identity.requests.ListRegionSubscriptionsRequest = { tenancyId: tenancyId }; const regions = await identityClient.listRegionSubscriptions(listRegionSubscriptionsRequest); return regions.items.map(region => { return region.regionName; }); } (async () => { identityClient = await new identity.IdentityClient({ authenticationDetailsProvider: provider }); const regions = await getSubscriptionRegions(compartmentId); console.log("Currently subscribed to the following region(s): ", regions) })();
- Compile the
example:
# use the TypeScript compiler to compile the example tsc region_subscriptions_example.ts
- Run the
example:
# run the example using node node region_subscriptions_example.js