Generate Tokens for Dev Portal Embeds
The Speakeasy SDK can generate access tokens for Embedded Components. For documentation on how to configure filters, find that HERE.
Below are examples for how to generate access tokens:
- Go
- Java
- Typescript
- Rust
import "github.com/speakeasy-api/speakeasy-schemas/grpc/go/registry/embedaccesstoken"
ctx := context.Background()
// If the SDK is configured as a global instance,
// an access token can be generated using the
// `GenerateAccessToken` function on the speakeasy package.
accessToken, err := speakeasy.GetEmbedAccessToken(ctx, &embedaccesstoken.EmbedAccessTokenRequest{
Filters: []*embedaccesstoken.EmbedAccessTokenRequest_Filter{
{
Key: "customer_id",
Operator: "=",
Value: "a-customer-id",
},
},
})
// If you have followed the `Advanced Configuration` section above,
// you can also generate an access token using the
// `GenerateAccessToken` function on the sdk instance.
accessToken, err := storeSDKInstance.GetEmbedAccessToken(ctx, &embedaccesstoken.EmbedAccessTokenRequest{
Filters: []*embedaccesstoken.EmbedAccessTokenRequest_Filter{
{
Key: "customer_id",
Operator: "=",
Value: "a-customer-id",
},
},
})
// Or finally if you have a handler that you would like to generate an access token from,
// you can get the SDK instance for that handler from the middleware controller
// and use the `GetEmbedAccessToken` function it.
func MyHandler(w http.ResponseWriter, r *http.Request) {
ctrl := speakeasy.MiddlewareController(req)
accessToken, err := ctrl.GetSDKInstance().GetEmbedAccessToken(ctx, &embedaccesstoken.EmbedAccessTokenRequest{
Filters: []*embedaccesstoken.EmbedAccessTokenRequest_Filter{
{
Key: "customer_id",
Operator: "=",
Value: "a-customer-id",
},
},
})
// the rest of your handlers code
}
@GetMapping("embed_access_token")
public String getSpeakeasyEmbedAccessToken(@RequestAttribute(SpeakeasyInterceptor.ControllerKey) SpeakeasyMiddlewareController controller){
String customerId=null;
// populate your customerId
// Restrict data by customer id
SpeakeasyEmbedAccessTokenRequestBuilder requestBuilder = new SpeakeasyEmbedAccessTokenRequestBuilder();
requestBuilder.withCustomerFilter(customerId);
// Restrict data by time (last 24 hours)
Instant startTime=Instant.now().minusSeconds(60*60*24);
requestBuilder.withTimeFilter(startTime,SpeakeasyEmbedAccessTokenRequestFilterOperator.GreaterThan);
String embedAccessToken=controller.getEmbedAccessToken(requestBuilder.build());
// build response
}
import { EmbedAccessTokenRequest } from "@speakeasy-api/speakeasy-schemas/registry/embedaccesstoken/embedaccesstoken_pb";
// If the SDK is configured as a global instance, an access token can be generated using the `generateAccessToken` function on the speakeasy package.
const req = new EmbedAccessTokenRequest();
const filter = new EmbedAccessTokenRequest.Filter();
filter.setKey("customer_id");
filter.setOperator("=");
filter.setValue("a-customer-id");
req.setFiltersList([filter]);
const accessToken = await speakeasy.getEmbedAccessToken(req);
// If you have followed the `Advanced Configuration` section above you can also generate an access token using the `GenerateAccessToken` function on the sdk instance.
const req = new EmbedAccessTokenRequest();
const filter = new EmbedAccessTokenRequest.Filter();
filter.setKey("customer_id");
filter.setOperator("=");
filter.setValue("a-customer-id");
req.setFiltersList([filter]);
const accessToken = await storeSDK.getEmbedAccessToken(req);
// Or finally if you have a handler that you would like to generate an access token from, you can get the SDK instance for that handler from the middleware controller and use the `GetEmbedAccessToken` function it.
app.all("/", (req, res) => {
const req = new EmbedAccessTokenRequest();
const filter = new EmbedAccessTokenRequest.Filter();
filter.setKey("customer_id");
filter.setOperator("=");
filter.setValue("a-customer-id");
req.setFiltersList([filter]);
const accessToken = await req.controller.getSDKInstance().getEmbedAccessToken(req);
// the rest of your handlers code
});
Coming Soon!