capture-customerids
description: Enable customer centric views and portal features
sidebar_position: 2
Capture CustomerIDs
Capturing CustomerIDs is not required, but is highly recommended. By setting a CustomerID you can associate requests to your customers/users, which powers the filters needed to build a customer-facing API dashboard.
If you are planning to build a developer experience portal, then it's important to pick a CustomerID that is stable, and connected to your identity system. When users login to your portal, Speakeasy will then be able to filter the data to just those relevant to the user.
For example: CustomerID is preferable to api_key because an API key can be rotated and is therefore unstable.
To associate requests with a customer ID, you need to instantiate the Speakeasy middleware in your handler functions and then set the CustomerID field. Below are per language examples of setting a CustomerID in a handler function.
- Go
- Java
- Typescript
- Rust
func MyHandler(w http.ResponseWriter, r *http.Request) {
ctrl := speakeasy.MiddlewareController(req)
ctrl.CustomerID("a-customers-id") // This customer ID will be used to associate this instance of a request with your customers/users
// the rest of your handlers code
}
@GetMapping("/v1/users/{id}") // The path template "/v1/users/{id}" is captured automatically by the SDK
public String getUser(@PathVariable("id") String id, @RequestAttribute(SpeakeasyInterceptor.ControllerKey) SpeakeasyMiddlewareController controller) {
controller.setCustomerID("a-customers-id"); // This customer ID will be used to associate this instance of a request with your customers/users
// your handler logic here
}
const app = express();
app.use(speakeasy.expressMiddleware());
app.all("/", (req, res) => {
// Provide a path hint for the request using the OpenAPI Path Templating format: https://swagger.io/specification/#path-templating-matching
req.controller.setCustomerID("a-customers-id"); // This customer ID will be used to associate this instance of a request with your customers/users
// the rest of your handlers code
});
#[post("/index")]
async fn index(controller: ReqData<Arc<RwLock<MiddlewareController>>>) -> HttpResponse {
controller
.write()
.unwrap()
.set_customer_id("123customer_id".to_string());
// rest of the handlers code
}