Anda dapat menambahkan kode dalam bahasa prosedural seperti JavaScript, Java, dan Python, lalu memanggil
kode tersebut dari bagian lain proxy API. Dukungan untuk kode prosedural dirancang untuk mempermudah Anda menerapkan penanganan kompleks pada variabel alur, kesalahan, serta isi permintaan dan respons.
Dengan kode prosedural, Anda dapat:
Membuat atau memanipulasi nilai isi yang kompleks, seperti nilai permintaan dan respons.
Menulis ulang URL, seperti untuk menyamarkan URL endpoint target.
Menambahkan logika lain yang tidak tersedia dengan kebijakan yang disertakan.
Cara kerjanya
Anda menggunakan kebijakan khusus untuk bahasa tersebut agar dapat melakukan panggilan dari alur proxy ke kode yang disertakan. Untuk setiap bahasa yang didukung, Apigee menyertakan kebijakan terpisah:
kebijakan JavaScript, kebijakan JavaCallout, dan kebijakan PythonScript.
Berikut ini menjelaskan, secara umum, salah satu cara kerjanya. Pastikan untuk melihat referensi kebijakan untuk mengetahui detail selengkapnya.
Dalam file terpisah, tulis kode yang akan Anda gunakan.
Dalam kode Anda, masukkan hasil pemrosesan kode ke dalam variabel alur Apigee.
Anda akan mengambil nilai variabel di tempat lain dalam proxy API. Untuk mengetahui informasi selengkapnya tentang variabel alur, lihat Mengelola status proxy dengan variabel alur.
Tambahkan file (atau JAR) sebagai file resource ke proxy API Anda.
Untuk mengetahui informasi selengkapnya tentang file resource, lihat File resource.
Tambahkan kebijakan yang sesuai dengan bahasa yang Anda gunakan.
Konfigurasi kebijakan untuk melakukan panggilan ke kode Anda dari tempat yang diinginkan dalam alur proxy API Anda.
Gunakan kebijakan untuk mengambil nilai kode dari variabel alur.
Batasan
Model keamanan Apigee tidak mengizinkan panggilan sistem seperti I/O jaringan, baca/tulis sistem file, info pengguna saat ini, daftar proses, dan penggunaan CPU/memori. Meskipun beberapa panggilan tersebut mungkin berfungsi, panggilan tersebut tidak didukung dan dapat dinonaktifkan secara aktif kapan saja. Untuk
kompatibilitas ke depan, sebaiknya hindari melakukan panggilan tersebut dalam kode Anda.
Batasan lain mungkin ada untuk setiap bahasa yang didukung, sebagaimana dijelaskan dalam referensi kebijakan yang sesuai untuk bahasa tersebut.
Bahasa yang didukung
JavaScript
Dukungan untuk JavaScript mencakup model objek lengkap untuk menangani variabel dan nilai khusus
untuk proxy API. Untuk mengetahui informasi selengkapnya, lihat Model objek JavaScript.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-05 UTC."],[[["\u003cp\u003eThis content covers how to use procedural code (JavaScript, Java, and Python) within Apigee and Apigee hybrid API proxies to manage flow variables, faults, and request/response bodies.\u003c/p\u003e\n"],["\u003cp\u003eApigee provides specific policies (JavaScript, JavaCallout, and PythonScript) to call out to your procedural code, which can be used to implement complex logic or rewrite URLs.\u003c/p\u003e\n"],["\u003cp\u003eProcedural code allows for the manipulation of complex body values, and logic not available through built-in policies, with results stored in flow variables for retrieval elsewhere in the API proxy.\u003c/p\u003e\n"],["\u003cp\u003eTo use procedural code, you write the code in a separate file, add it as a resource file to your API proxy, configure the relevant language policy to call the code, and retrieve the results using flow variables.\u003c/p\u003e\n"],["\u003cp\u003eThe security model in Apigee restricts certain system calls in procedural code, such as network I/O and file system operations, which should be avoided for forward compatibility.\u003c/p\u003e\n"]]],[],null,["# Incorporating procedural code\n\n*This page\napplies to **Apigee** and **Apigee hybrid**.*\n\n\n*View [Apigee Edge](https://docs.apigee.com/api-platform/get-started/what-apigee-edge) documentation.*\n\nYou can add code in procedural languages such as JavaScript, Java, and Python, then call out\nto that code from other parts of an API proxy. Support for procedural code is designed to make it\neasier for you to implement complex handling of flow variables, faults, and request and response\nbodies.\n\nWith procedural code, you can:\n\n- Create or manipulate complex body values, such as request and response values.\n- Rewrite URLs, such as to mask a target endpoint URL.\n- Add other logic not available with included policies.\n\nHow it works\n------------\n\nYou use a policy specific to the language in order to make calls from a proxy flow to your\nincluded code. For each of the supported languages, Apigee includes a separate policy:\n[JavaScript policy](/apigee/docs/api-platform/reference/policies/javascript-policy), [JavaCallout policy](/apigee/docs/api-platform/reference/policies/java-callout-policy), and [PythonScript policy](/apigee/docs/api-platform/reference/policies/python-script-policy).\n\nThe following describes, at a high level, one way this works. Be sure to see the policy\nreferences for more details.\n\n1. In a separate file, write the code you're going to use.\n2. In your code, put the result of the code's processing into an Apigee flow variable. You'll retrieve the variable value elsewhere in your API proxy. For more about flow\n variables, see [Managing\n proxy state with flow variables](/apigee/docs/api-platform/fundamentals/introduction-flow-variables).\n\n3. Add your file (or JAR) as a resource file to your API proxy. For more about resource files, see [Resource files](/apigee/docs/api-platform/develop/resource-files).\n\n4. Add the policy corresponding to the language you're using. Configure the policy to make a call to your code from the desired place in your API\n proxy's flow.\n\n5. Use a policy to retrieve the code's value from the flow variable.\n\nLimitations\n-----------\n\nThe Apigee security model does not permit system calls such as network I/O, filesystem\nread/writes, current user info, process list, and CPU/memory utilization. Although some such\ncalls may be functional, they are unsupported and liable to be actively disabled at any time. For\nforward compatibility, you should avoid making such calls in your code.\n\nOther limitations may exist for each supported language, as described in the language's\ncorresponding policy reference.\n\nSupported languages\n-------------------\n\n### JavaScript\n\nSupport for JavaScript includes a full object model for handling variables and values specific\nto an API proxy. For more, see [JavaScript object model](/apigee/docs/api-platform/reference/javascript-object-model).\n\nFor policy reference information and examples, see [JavaScript policy](/apigee/docs/api-platform/reference/policies/javascript-policy).\n\n### Java\n\nUse the [JavaCallout\npolicy](/apigee/docs/api-platform/reference/policies/java-callout-policy) to make calls to Java code from your proxy's flow. The policy reference topic includes\nexamples.\n\n### Python\n\nUse the [Python Script\npolicy](/apigee/docs/api-platform/reference/policies/python-script-policy) to call out to Python code."]]