Study FAQ
How do I transform a custom connector response without masking an API failure?
Also asked as 2
- How can custom connector C# code flatten a successful response and preserve backend errors?
- What should connector code return when a successful HTTP response has the wrong shape?
Answer
Use custom code when a policy template cannot express the response transformation. In the supported C# script, implement Script : ScriptBase and override ExecuteAsync. Forward the request with Context.SendAsync. Preserve the backend's error statuses. For a successful response, validate the shape before converting it to the consumer's documented schema. If a 200 response is missing required fields or has the wrong types, return a deliberate contract error such as 502 rather than inventing an empty success.
For example, a quote API might wrap its result in result. Validate that the inner quote ID, numeric cost, and currency meet the exposed contract before returning the flattened fields. Restrict the code to the intended operation. If dispatch uses the operation identifier, consult the documented regional encoding issue and validate the identifier handling.
Check the supported namespaces and platform limitations. Microsoft documents one script file, .NET Standard 2.0, up to 1 MB and a two-minute ceiling on new or updated connectors. Custom code has documented gateway and virtual-network limitations, including the public endpoint path used by Context.SendAsync.
Compile against the supported interfaces, deploy to a development connector, and test valid 200, malformed success, invalid credentials, and a controlled service failure. Verify the actual downstream flow branches and output schema with code enabled.
Microsoft references
Full explanation
AB-400: Custom Connectors with OpenAPI, Policies and Code
Study guide · AB-400
Read the full explanation