Customize API Request Headers

By: JP Nate | Asked: 01/29/2025
ForumsCategory: Code HelpCustomize API Request Headers
JP Nate asked 1 day ago

Hi, I'm having issues customizing API request headers in a dynamic way.  I have a form that needs to send entries to a third party CRM using their API. I've done some tests and I have confirmed that it works when I use this:

add_filter( 'frm_api_request_args', 'my_custom_frm_api_request_header', 10, 2 );
function my_custom_frm_api_request_header( $arg_array, $args ) {
  if ( $args['url'] == 'http://test.com' ) { // my full url where the request is being sent
    $arg_array['headers']['Authorization'] = 'Bearer MY_TOKEN';
    $arg_array['headers']['Content-Type'] = 'application/json';
  }
  return $arg_array;
}

This is from the documentation found here: https://formidableforms.com/knowledgebase/frm_api_request_args/

However, I have multiple TOKENS and I need to be able to assign the TOKEN dynamically in the header based on some other element of the form, like the form ID or some other key/value entered in the form settings.  Or, maybe the name I enter under the Send API Data setting? I'm open, but just need any ideas on how this might work? I've searched the internet for hours now and have had no luck.

This was my attempt at using the form ID method, but the Formidable Log gave a '401 Authorization error':

add_filter( 'frm_api_request_args', 'my_custom_frm_api_request_header', 10, 2 );
function my_custom_frm_api_request_header( $arg_array, $args ) {
    // Make sure the form object exists
    if ( isset( $args['form_id'] ) ) {
        $form_id = $args['form_id']; // Get the Form ID
        
        // Assign Project Tokens based on Form ID
        if ( $form_id == 23 ) {
            $arg_array['headers']['Authorization'] = 'Bearer TOKEN_FOR_FORM_23';
        } elseif ( $form_id == 45 ) {
            $arg_array['headers']['Authorization'] = 'Bearer TOKEN_FOR_FORM_45';
        } elseif ( $form_id == 67 ) {
            $arg_array['headers']['Authorization'] = 'Bearer TOEN_FOR_FORM_67';
        }

        // Always set Content-Type as JSON
        $arg_array['headers']['Content-Type'] = 'application/json';
    }

    return $arg_array;
}

Just to reiterate, I can get this to work by just entering one TOKEN, so all of my current setup is correct. I just need a way to properly write the code to customize the headers in a dynamic way that will work with Formidable's "frm_api_request_args". Appreciate the help!

Victor Font Staff replied 13 hours ago

API Keys are one per site. You can't have multiple forms on the same site use different API keys. Where did you get that idea?

JP Nate replied 13 hours ago

Apologies for the confusion, I've edited my original post. I meant multiple Bearer Tokens. The CRM API calls them keys and my brain added "API" before the word "keys" while I was typing last night.

1 Answers
Victor Font Staff answered 9 hours ago

Did you examine the content of $args['form_id'] is an array element?

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right