Installing the Enforcer

Communicate with the PerimeterX Backend

In order for the rust Enforcer to communicate with the PerimeterX Collector server, a specific server must be added and configured in the Fastly UI (or using contrib/ script).
Backend parameters (replace ${APP_ID} with your PerimeterX Application ID):

  • Name: PX_BACKEND_${APP_ID}
  • Hostname: sapi-${APP_ID}
  • Override host: sapi-${APP_ID}
  • Use SSL/TLS: Yes

Module integration

perimeterx-fastly-enforcer dependency must be included in Cargo.toml :

perimeterx-fastly-enforcer = { path = "../perimeterx-fastly-enforcer" }

(adjust path parameter to perimeterx-fastly-enforcer folder location.)

To integrate PerimeterX Rust module into an existing Rust code, the following base snippet could be used:

    let mut px: PXEnforcer = PXEnforcer::new();
    let px_result = px.enforce(&mut req)?;
    if let Some(r) = px_result {
        return Ok(r);

    //... communicate with Origin server / process request and response


Enforcer API

Initialize PXEnforcer structure:

pub fn new() -> Self

This function takes a request and returns a result which basically contains a response (for "blocked" or "first party" requests):

pub fn enforce(&mut self, req: &mut Request) -> Result<Option<Response>, Error>

At the end of request processing, the following function must be called to finalize PerimeterX enforcer code:

pub fn post_enforce(&self, res: &Response)

It is possible to access PXContext structure with various Enforcer variables via px.ctx member:

    // send "score" value to the Origin
    req.set_header("x-px-score", px.ctx.score.to_string());

To set "custom_parameters" variables, the following callback function could be used:

pub type PXEnrichCustomParamsFn = fn (req: &Request, conf: &PXConfig, params: &mut PXCustomParams);

req: fastly::Request
conf: PXConfig
params: modifiable structure with custom_param1 .. custom_param10 fields

To set custom parameters callback function, use the following setter:

pub fn set_enrich_custom_params_fn(&mut self, f: PXEnrichCustomParamsFn)

Sample code

This is the simplest example on how to use PerimeterX Rust module:

use fastly::{Error, Request, Response};
use perimeterx_fastly_enforcer::pxenforce::PXEnforcer;

const ORIGIN_BACKEND: &str = "origin_backend";

fn send_to_origin(req: Request) -> Result<Response, Error> {
    println!("sending to Origin...");
    match req.send(ORIGIN_BACKEND) {
        Ok(r) => return Ok(r),
        Err(e) => return Err(e.into()),

fn main(mut req: Request) -> Result<Response, Error> {

    let mut px: PXEnforcer = PXEnforcer::new();

    // execute PX Enforcer for Request
    let px_result = px.enforce(&mut req)?;

    // return, if it's a "blocked" or "first party" response
    if let Some(r) = px_result {
        return Ok(r);

    // ... process Client request ...

    // we can access "PXContext" structure.
    // as an example: send "score" value to the Origin
    req.set_header("x-px-score", px.ctx.score.to_string());

    // a client function to communicate with the Origin
    let response = send_to_origin(req)?;

    // ... process Origin response ...

    // must be called at the end

    // we are ok to send response back to client
    return Ok(response);

Did this page help you?