Original Message:
Sent: Tue November 18, 2025 05:50 AM
From: Shah Wajahat
Subject: How to pass context varaiblr to a policy
Hi karthik kumar,
I have already created rate limit object in gateway stg01 with as you see in my screenshot above.
but when we use this CCMSRQ001 
but when pass context variable it gives an error show as above.
------------------------------
Shah Wajahat
Original Message:
Sent: Mon November 17, 2025 11:06 AM
From: karthik kumar
Subject: How to pass context varaiblr to a policy
That error does not come from your GatewayScript.The error is not related to context variables, even if serviceId was empty, you would NOT get this error.This error appears before execution when APIC validates the rate-limit configuration. It comes from API Connect / DataPower when the rate-limit policy is deployed.
Your API assembly references a Rate Limit object by name
But the DataPower gateway (Stg-Int01) does NOT have that Rate Limit object defined.
Common Error :
Creating the rate-limit in Sandbox or API Manager drafts, but not publishing it to the real gateway.
Each gateway needs its own copy..
Even though you created the rate-limit object somewhere, it is not deployed to that gateway.
You must:
1. Go to API Manager → Drafts → Gateway Services
Find the gateway:Stg-Int01
2. Open it → Rate Limits tab
Check if your rate-limit object (the one used in assembly) exists.
If it is missing:
Click Add
Create a rate limit with the SAME NAME used in the API assembly.
------------------------------
karthik kumar
Original Message:
Sent: Mon November 17, 2025 06:55 AM
From: Shah Wajahat
Subject: How to pass context varaiblr to a policy
Hi karthik kumar
I updated my gatewayscript code as you requested show in below
var apim - require('apim')
var body - apin.getvariable('message.body');
var bankId - body.item(0).getElementsByTagNane("bankId").item(0).textContent;
var serviceld - body.item(0).getElementsByTagName("serviceId").item(0).textContent;
apim.setvariable('context.serviceId' ,serviceld);
apim.setvariable("body",body);
I am using the gatewayscript before the ratelimit policy as show in screenshot

But I get an error

I am passing my context value like this

I have defined the rate limit object in DataPower.

------------------------------
Shah Wajahat
Original Message:
Sent: Mon November 17, 2025 01:17 AM
From: karthik kumar
Subject: How to pass context varaiblr to a policy
In IBM API Connect (APIC), you can use Gateway Script variables inside policies such as Rate Limit, but only if you store the value in a context variable that APIC makes available to the assembly.
var apim = require('apim')
var body = apim.getvariable('message.body');
var bankId = body.item(0).getElementsByTagName("bankId").item(0).textContent;
var serviceId = body.item(0).getElementsByTagName("serviceId").item(0).textContent;
apim.setvariable("serviceId", serviceId);
This correctly stores the variable into the context.
apim.setvariable('context.serviceId', serviceId);using the context. prefix is best practice
Open the Rate Limit policy → Advanced section → Configure "Rate Limit By".
Use custom key then use the context variable like this: $(context.serviceId) or if you stored it without prefix:$(serviceId)
Rate Limit Policy
Rate Type: per Consumer / per Key (depending on your use case)
Rate Limit Key:
$(context.serviceId) This makes the rate limit apply per serviceId, meaning each serviceId gets its own bucket.
Make sure the GatewayScript policy is placed above the Rate Limit policy in the assembly flow.
For XML body
Note that message.body is a DOM Node, so your use of getElementsByTagName() is correct.
------------------------------
karthik kumar
Original Message:
Sent: Wed November 12, 2025 08:56 AM
From: Shah Wajahat
Subject: How to pass context varaiblr to a policy
Hi Teams,
I want to know about how I can used my Context variable my which is set in gateway script
var apim = require('apim')
var body = apim.getvariable('message.body');
var bankId = body.item(0).getElementsByTagName("bankId").item(0).textContent;
var serviceId = body.item(0).getElementsByTagName("serviceId").item(0).textContent;
apim.setvariable("serviceId",serviceId);
I want to be used seviceId value on assembly rate-limite

I have tried this $(serviceId) and also this serviceId it didn't work.
Please suggested how can I used it.
Thank you,
------------------------------
Shah Wajahat
------------------------------