Files
help365/platform/api-method-with-wildcard.html
2025-05-27 21:32:35 +04:00

249 lines
22 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<title>Use API methods with wildcards in modules</title>
<meta name="generator" content="Help+Manual" />
<meta name="keywords" content="" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="When calling an API method in a module, you can send parameter values to its script and process them. For example, in the method URL, you can specify the identifier of the file..." />
<meta name="picture" content="" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Full documentation for BRIX365 platform. Low-code developer guide. User guide. Admin guide. Developer guide." />
<meta property="og:url" content="https://brix365.com/en/help" />
<meta property="og:image" content="" />
<link rel="icon" href="favicon.png" type="image/png" />
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" />
<link rel="stylesheet" href="./jquery-ui.min.css" />
<link rel="stylesheet" href="default.css" />
<link rel="stylesheet" href="./search-yandex.css" />
<link rel="stylesheet" href="./article.css" />
<link rel="stylesheet" href="./glossary.css" />
<link rel="stylesheet" href="./theme.css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="helpman_settings.js"></script>
<script type="text/javascript" src="helpman_topicinit.js"></script>
<script type="text/javascript" src="highlight.js"></script>
<script type="text/javascript">
$(document).ready(function(){highlight();});
</script>
</head>
<body>
<script>!function(e,t,c,n,r,a,m){e.ym=e.ym||function(){(e.ym.a=e.ym.a||[]).push(arguments)},e.ym.l=1*new Date;for(var s=0;s<document.scripts.length;s++)if(document.scripts[s].src===n)return;a=t.createElement(c),m=t.getElementsByTagName(c)[0],a.async=1,a.src=n,m.parentNode.insertBefore(a,m)}(window,document,"script","https://mc.yandex.ru/metrika/tag.js"),ym(83179930,"init",{clickmap:!0,trackLinks:!0,accurateTrackBounce:!0,webvisor:!0})</script><noscript><div><img alt=""src=https://mc.yandex.ru/watch/83179930 style=position:absolute;left:-9999px></div></noscript>
<header class="header elma-365">
<div class="container">
<a class="header__logo" href="https://brix365.com/en/help">
<img src="./logo-en.svg" alt="header logo">
</a>
<!-- <div class="hero__search-form" id="search-panel">
<form class="search-form" onsubmit="ym(83180416,'reachGoal','poisk')">
<label class="search-form__label">
<span id="reset-search" class="search__icon"></span>
<input class="search-form__input" type="text">
</label>
<input class="search-form__submit" type="submit" value="Submit">
</form>
</div> -->
<div class="hero__search-form" id="search-panel"> <form class="search-form"> <label class="search-form__label"> <span id="reset-search" class="search__icon"></span> <input class="search-form__input" type="text"> </label> <input class="search-form__submit" type="submit" value="Submit"> </form> </div>
<div class="hero__search">
<a href="#" id="search-icon" class="hero__search-icon">
<img src="search-icon-white.svg" alt="search string">
</a>
<a href="#" id="side-menu-icon" class="hero__side-icon">
<img src="side_menu.svg" alt="side menu">
</a>
</div>
<div class="header__navi">
<ul class="header__list"><li><span class="solution-select"><span class="solution-select__selected"></span><svg width="7" height="4" viewBox="0 0 7 4" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L3.5 3.5L6 1" stroke="white" stroke-linecap="round" stroke-linejoin="round"/></svg><ul class="solution-select__list"><li><a class="project-link" href="https://brix365.com/en/help/platform/get-trial.html">Platform</a></li><li><a class="project-link" href="https://brix365.com/en/help/ecm/ecm-functions.html">ECM</a></li><li><a class="project-link" href="https://brix365.com/en/help/crm/crm_overview.html">CRM</a></li><li><a class="project-link" href="https://brix365.com/en/help/service/service-functions.html">Service</a></li><li><a class="project-link" href="https://brix365.com/en/help/projects/projects-functions.html">Projects</a></li><li><a class="project-link" href="https://brix365.com/en/help/business_solutions/-elma365-store.html">Business Solutions</a></li></ul></span></li><li><a href="https://api.brix365.com/en/" target="_blank">API</a></li><li><a href="https://tssdk.brix365.com/" target="_blank">SDK</a></li></ul>
</div>
</div>
</header>
<main class="main container">
<aside class="sidebar" id="sidebar">
<div class="sidebar__header">
<a class="header__logo" href="https://brix365.com/en/help">
<img src="./logo-light-en.svg">
</a>
<span class="sidebar__close elma-365-close" id="close"></span>
</div>
<div class="sidebar__wrapper" id="side-menu">
</div>
</aside>
<article class="article" id="article">
<div class="article-inner">
<div class="content">
<header class="article__header">
<div class="article__bread" style="display:flex; gap:10px;">
<span id="subcategory" class="search-res__item-category search-res__item-category_subcategory subcategory article__badge"></span>
<div class="topic__breadcrumbs">
<p><a href="360024498352.html">Modules</a> &gt; <a href="extentions.html">Custom modules</a> &gt; <a href="extention-api.html">API methods in modules</a> / Use API methods with wildcards in modules</p>
</div>
</div>
<div class="topic__title"><h1 class="p_Heading1"><span class="f_Heading1">Use API methods with wildcards in modules</span></h1>
</div>
</header>
<section class="article__content">
<div class="scroll-top-inner">
<a href="#h1-article" class="scroll-top"></a>
</div>
<!-- Placeholder for topic body. -->
<p class="p_Normal">When calling an <a href="extention-api.html" class="topiclink">API method in a module</a>, you can send parameter values to its script and process them. For example, in the method URL, you can specify the identifier of the file to be retrieved or define further actions depending on the sent value.</p>
<p class="p_Normal">When calling a method this way, you can customize integration with third-party services that use complex request routing with parameters sent directly to the URL.</p>
<p class="p_Normal">To send values when calling an API method:</p>
<ul style="list-style-type:disc">
<li class="p_Normal">Set a special address for it.</li><li class="p_Normal">Configure the processing of values in the method script.</li></ul>
<p class="p_Normal">Let's see how to do it.</p>
<h2 class="p_Heading2"><a id="create-method" class="hmanchor"></a><span class="f_Heading2">Create a method with a wildcard</span></h2>
<p class="p_Normal">Let's assume you create an API method to get the names and emails of employees. When you call it, the ID of the user you want to request information about is sent to it. If no ID is sent, the response includes the data of all users.</p>
<p class="p_Normal">To configure such a method:</p>
<ol style="list-style-type:upper-roman">
<li value="1" class="p_Normal">In the custom module settings on the <span style="font-weight: bold;">API Methods</span> tab, go to editing methods.</li><li value="2" class="p_Normal">Click <span style="font-weight: bold;">+ Add</span>.</li><li value="3" class="p_Normal">In the window that opens, enter a name and select the <code><b>GET</b></code> method because the call will retrieve data.</li><li value="4" class="p_Normal">In the <span style="font-weight: bold;">Address</span><span style="font-weight: bold; color: #ff0000;">*</span> field, enter the path to the method and add the character combination <code><b>/*</b></code> at the end.<br />
<img alt="api-method-with-wildcard-1" width="754" height="426" style="margin:0;width:754px;height:426px;border:none" src="api-method-with-wildcard-1.png"/></li><li value="5" class="p_Normal">In the <span style="font-weight: bold;">Function</span><span style="font-weight: bold; color: #ff0000;">*</span> field, specify the name of the function containing the method script and click <span style="font-weight: bold;">Save</span>.</li><li value="6" class="p_Normal">Save the API method. Now, the part of the address that will be in the request instead of the <code><b>*</b></code> symbol is not taken into account in routing. You can send parameters in it.</li><li value="7" class="p_CodeExample" style="white-space: normal; page-break-inside: avoid;">Go to the <span style="font-weight: bold;">Scripts</span> tab. Determine what actions will be performed in the method depending on the sent value. To do this, get the value from the <code><b>fragment</b></code> property of the <code><b>HttpApiRequest</b></code> type, which the API method accepts when called:</li></ol>
<p class="p_CodeExample" style="white-space: normal; page-break-inside: avoid;"><span class="f_CodeExample">async function users(req: HttpApiRequest): Promise&lt;HttpResponse | void&gt; {</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;const resp = new HttpResponse();</span><br />
<span class="f_CodeExample">&nbsp;</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;// Write the User ID received when calling a method in the `userId`variable</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;const userId = req.fragment;</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;// If the ID is not empty, search for a specific user</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;if (userId) {</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;const user = await System.users.search().where((f) =&gt; f.__id.eq(userId)).first();</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;// If the user is found, send their data in the response</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;if (user) {</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resp.status(HttpStatusCode.OK);</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resp.json({</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;id: user.data.__id,</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;name: user.data.__name,</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email: user.data.email,</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;// If the user is not found, send an error message in the response</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;} else {</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resp.status(HttpStatusCode.NOT_FOUND);</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resp.content(`user with id ${ userId } not found`);</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;return resp;</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;}</span><br />
<span class="f_CodeExample">&nbsp;</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;// If the ID is not sent in the request, send data of all users in the response</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;const users = await System.users.search().all();</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;resp.status(HttpStatusCode.OK);</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;resp.json(users.map((user) =&gt; ({</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;id: user.data.__id,</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;name: user.data.__name,</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;email: user.data.email,</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;})));</span><br />
<span class="f_CodeExample">&nbsp;</span><br />
<span class="f_CodeExample">return resp;</span><br />
<span class="f_CodeExample">}</span></p>
<p class="p_Normal">Read more about how to write scripts for API methods in the <a href="extention-api.html#access-to-global-constants" class="topiclink">API methods in modules</a> article.</p>
<ol style="list-style-type:upper-roman">
<li value="8" class="p_Normal">Save and publish the method.</li></ol>
<p class="p_Normal">Now, you can call the configured API method from external systems or in BRIX scripts.</p>
<h2 class="p_Heading2"><span class="f_Heading2">Call a method with a wildcard from an external service</span></h2>
<p class="p_Normal">To call API methods from external services, use the <code><b>fetch()</b></code> method. Send the full URL of the API method to it. To specify parameter values in the request, add them at the end of the URL using the <code><b>/</b></code> symbol.</p>
<p class="p_Normal">For example, when calling the <a href="api-method-with-wildcard.html#create-method" class="topiclink">users/* method</a>, the user ID is sent to get user data:</p>
<p class="p_CodeExample" style="white-space: normal; page-break-inside: avoid;"><span class="f_CodeExample">let response = await fetch('https://abcd1234.brix.eu/api/extensions/f1e6cf15-7c65-4be5-8443-021a280dcd80/script/users/fb894dcb-9fe0-4783-b7a9-8a76099446b0', {</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;method: &quot;GET&quot;,</span><br />
<span class="f_CodeExample">});</span></p>
<h2 class="p_Heading2"><span class="f_Heading2">Call a method with a wildcard in a BRIX script</span></h2>
<p class="p_Normal">To call API methods in scripts, use the <code><b>call()</b></code> method. To send parameter values, use the <code><b>withFragment()</b></code> function.</p>
<p class="p_Normal">For example, when calling the <a href="api-method-with-wildcard.html#create-method" class="topiclink">users/* method</a>, you can specify the user ID to get their data:</p>
<p class="p_CodeExample" style="white-space: normal; page-break-inside: avoid;"><span class="f_CodeExample">await Namespace.api.users</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;.withFragment('fb894dcb-9fe0-4783-b7a9-8a76099446b0')</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;.call({</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp; &nbsp; &nbsp;method: 'GET',</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;});</span></p>
<p class="p_Normal">If you do not send any parameters, you can call the method without the <code><b>withFragment()</b></code> function.</p>
<p class="p_Normal">For example, when calling the <code><b>users/*</b></code> method in such a way, you get data about all employees:</p>
<p class="p_CodeExample" style="white-space: normal; page-break-inside: avoid;"><span class="f_CodeExample">await Namespace.api.users.call({</span><br />
<span class="f_CodeExample"> &nbsp; &nbsp;method: 'GET',</span><br />
<span class="f_CodeExample">});</span></p>
<div class="bottom-nav">
<a id="prev-link" class="topic__navi_prev" href="extention-api.html">
<span class="bottom-nav__arrow bottom-nav__arrow--prev"></span> <span
class="bottom-nav__link">extention-api.html</span>
</a>
<a id="next-link" class="topic__navi_next" href="extention-activities.html">
<span class="bottom-nav__link">extention-activities.html</span> <span
class="bottom-nav__arrow bottom-nav__arrow--next"></span>
</a>
</div>
<!-- добавляет на страницу строку блок Была ли статья полезной? -->
<div class="feedback" id="feedback"><div class="feedback-help"><span><b>Was this helpful?</b></span><form action="" method="POST" class="feedback-form" id="feedback-form"><div class="feedback__popup feedback__popup-response" id="feedback__popup_thx" style="display: none;">Thanks for your feedback!</div><div class="feedback__popup" id="feedback__popup_why" style="display: none;"><div class="feedback__popup-header">Please specify why:</div><input type="radio" name="category" id="bad_recommendation" value="bad_recommendation"><label for="bad_recommendation">Recommendations did not help me</label><input type="radio" name="category" id="difficult_text" value="difficult_text"><label for="difficult_text">Article is hard to understand</label><input type="radio" name="category" id="no_answer" value="no_answer"><label for="no_answer">Didn`t answer my question</label><input type="radio" name="category" id="bad_header" value="bad_header"><label for="bad_header">Content does not match the topic</label><input type="radio" name="category" id="other_reason" value="other_reason"><label for="other_reason">Other</label></div><div class="feedback__popup" id="feedback__popup-other" style="display: none;"><div class="feedback__popup-header">How we can improve it?</div><textarea class="feedback__textarea" name="other" id=""></textarea><input type="submit" class="feedback__other-btn" value="Submit"></div><div class="feedback-form__btn-group"><input type="radio" name="useful" id="feedback__useful_yes" value="true"><label for="feedback__useful_yes"><img src="like.svg" class="small-img" alt="like"><spanclass="feedback-form__btn-group_yes-btn">Yes</spanclass="feedback-form__btn-group_yes-btn"></label><input type="radio" name="useful" id="feedback__useful_no" value="false"><label for="feedback__useful_no"><img src="dislike.svg" class="small-img" alt="dislike"><spanclass="feedback-form__btn-group_no-btn">No</spanclass="feedback-form__btn-group_no-btn"></label></div><select name="category"><option disabled="">Please specify why</option><option value="bad_recommendation" selected="">Recommendations did not help me</option><option value="difficult_text">Article is hard to understand</option><option value="no_answer">Didn`t answer my question</option><option value="bad_header">Content does not match the topic</option><option value="other_reason">Other</option></select><input type="submit"></form></div><div class="found_typo"><p style="margin: 0px; margin-top: 16px !important;"><span><b>Found a typo?</b></span> Select it and press <i>Ctrl+Enter</i> to send us feedback</p></div></div>
</section>
</div>
<aside class="article__sidebar" style="display:none">
<input type="checkbox" />
<div class="article__arrow"></div>
<div class="table-of-contents elma365-right" id="toc2Content">
<h3 class="h3-toc">In this topic</h3>
<nav id="toc2"></nav>
</div>
</aside>
</div>
</article>
</main>
<footer class="footer">
<div class="footer-container">
<div class="footer-mobile">
<ul class="footer-mobile__list"><li><a href="https://brix365.com/en/" target="_blank">BRIX</a></li><li><a href="https://tssdk.brix365.com/en/latest/" target="_blank">SDK</a></li><li><a href="https://api.brix365.com/en/" target="_blank">API</a></li></ul><ul class="footer-mobile__list"><li><a href="https://brix365.com/en/help/platform/get-trial.html">Platform</a></li><li><a href="https://brix365.com/en/help/ecm/ecm-functions.html">ECM</a></li><li><a href="https://brix365.com/en/help/service/service-functions.html">Service</a></li><li><a href="https://brix365.com/en/help/projects/projects-functions.html">Projects</a></li></ul>
</div>
<div class="footer-wrap">
<div><span class="mobile-question-popup">Send feedback</span><form method="POST" action class="question__popup question-xs" id="question__popup"><div class="question-wrap"><span class="close"></span><span class="title">Ask a question</span><label for="help_question" style="display: none;"></label><textarea name="help_question" id="help_question"></textarea><input type="submit" value="Send"></div></form><div class="hidden fade-in question-success-xs">Sent</div></div>
<div class="footer-flex-b">
<span class="footer-copy">&copy; 2025 BRIX</span>
<ul class="footer-list">
<li class="footer-item">
<a href="#" class="arrow-top" style="display: block;"></a>
</li>
</ul>
</div>
</div>
</div>
</footer>
<iframe name="hmnavigation" style="display:none!important"></iframe>
<script src="./jquery-ui.js"></script>
<!--script src="//cdn.jsdelivr.net/npm/featherlight@1.7.14/release/featherlight.min.js" type="text/javascript" charset="utf-8"></script-->
<script src="./jquery.tocify.min.js"></script>
<script src="./TypoReporter.min.js"></script>
<script src="./google-search.js"></script>
<script src="./main.js"></script>
</body>
</html>