A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learning.postman.com/docs/tests-and-scripts/write-scripts/packages/package-library/ below:

Add internal scripts to the Package Library in Postman

With the Package Library, you can maintain scripts and tests in a central location and share them with your team. Add commonly used scripts and tests to packages in your team's Package Library, and reuse them in your internal workspaces. The Package Library supports HTTP, gRPC, and GraphQL requests.

About the Package Library

Use the Package Library to store commonly used scripts and tests as packages in your Postman team. Your teammates can access and import packages from the Package Library, enabling you to share scripts and tests directly in Postman. The Package Library supports JavaScript code.

The number of packages your team can store in the Package Library depends on your Postman plan. Learn more about resource usage in Postman.

Import packages into the Scripts tab in your team's HTTP, gRPC, or GraphQL requests. The contents of packages will run when you run the requests they're imported in. Learn more about the run order of scripts.

The contents of packages will also run when you run HTTP collections from the Collection Runner (manual and scheduled), Monitors, and Postman Flows. If you're on a Professional or Enterprise team, the contents of packages will run when you run HTTP collections from the Postman CLI. Note that the contents of packages won't run from Newman.

You can learn about importing packages from an external package registry.

Add a package

You can create a package from scratch in the Package Library. You can also add existing code to a new package or an existing package. You can only open the Package Library from internal workspaces. To use a package, write code in a package, then import the package into your scripts.

You are the owner of the packages you create. Packages remain in your team's Package Library when a package's owner leaves the team or is removed from the team.

Add a new package

To add a new package, do the following:

  1. Open an HTTP collection, folder, or request. You can also open a gRPC or GraphQL request.

  2. Select the Scripts tab.

  3. Select Packages at the lower right of the code editor, and select Open package library.

  4. Select New Package.

  5. Enter the following:

  6. Select Create.

Add existing code to a package

To add existing code to a package, do the following:

  1. Open an HTTP collection, folder, or request. You can also open a gRPC or GraphQL request.

  2. Select the Scripts tab.

  3. Write code you want to add to a package. Learn how to write code in a package.

  4. Highlight the code, then right-click the highlighted code.

  5. You can add existing code to a package in the following ways:

  6. Update the package Name, Summary, and Code as needed.

    You can't use the name of a package that already exists or a package that was deleted. Also you can't change the name of a package later.

  7. Select Save.

To make your code more readable, you can select Beautify in the lower right of the code editor.

Write code in a package

Add JavaScript code, functions, and objects to packages in your team's Package Library. You can also use the Postman JavaScript API functionality, enabling you to use the pm object to access and alter request and response data, write test assertions, and more. The contents of packages run in the order they're listed. After you write code in a package, you can import a package.

You can't use the previous style of writing Postman tests in packages in the Package Library.

Export your functions and objects using the module.exports object property, enabling you to call them from your scripts. The name you export must match the name of the function declaration or object in your package.

function functionName {
    return result
}

module.exports = {
    functionName
}

You can learn how to document JavaScript functions in your packages.

If your package only includes JavaScript code or pm object instances, and no callable functions or objects, you don't need to export anything.

The following example is a package named postman_logger:


function logger (data) {
    console.log(`Logging information to the console, ${data}`)
}

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});

module.exports = {
    logger
}
Add documentation to a package

Postman supports JSDoc for documenting JavaScript functions in your packages. Documentation added to your functions using JSDoc will display in a popup window when you call each function from your scripts. You can use the official JSDoc documentation to learn how to add documentation to your packages.

The following example has documentation for the logger function using JSDoc. The documentation explains what the function does, and defines what the data parameter is used for and that it accepts a string data type.


function logger (data) {
    console.log(`Logging information to the console, ${data}`)
}

module.exports = {
    logger
}
Import a package

Once you write code in a package, you can import a package into your scripts. This enables you to access the scripts and tests in your packages, and call specific functions and objects. The contents of packages will only run from internal workspaces.

You can't import a package into the contents of another package in the Package Library.

Select Packages at the lower right of the code editor. Search for packages to import into the code editor. Select Postman packages to filter your search results by packages in your team's Package Library. Select a package to import it into the code editor.

Packages are imported into the code editor using the pm.require method. Your team domain and the package name are included as the argument in the following format: @team-domain/package-name. This also declares a JavaScript variable that you can use to call functions and objects in your packages. By default, the variable identifier is based on the package name.

If you change your team domain in your publisher profile, you must also change the team domain in each occurrence of the pm.require method. You can manually update the team domain, or re-import each package.

const variableName = pm.require('@team-domain/package-name');

variableName.functionName()

If your package only includes JavaScript code or pm object instances, and no callable functions or objects, you only need to import it in your scripts. You don't need to also declare it as a JavaScript variable.

pm.require('@team-domain/package-name');

To view all packages imported in your request's script, select Packages at the lower right of the code editor. Packages with a blue checkmark are imported in your script.

The following example imports a package named postman_logger:



const postmanLogger = pm.require('@postman/postman_logger');

postmanLogger.logger("The test passed")


Edit a package

All team members can view and edit packages in your Postman team.

To edit a package, do the following:

  1. Open an HTTP collection, folder, or request. You can also open a gRPC or GraphQL request.
  2. Select the Scripts tab.
  3. Select Packages at the lower right of the code editor, and select Open package library.
  4. Search for a package you'd like to edit.
  5. Edit the Summary and Code for the package as needed.
  6. Select Save.
Delete a package

You must be a Team or Super Admin or the package's owner, to delete a package from your team's Package Library. To find a package's owner, open the Package Library, then view the team member associated with a package.

You can't reuse the name of a package deleted from your Package Library.

To delete a package, do the following:

  1. Open an HTTP collection, folder, or request. You can also open a gRPC or GraphQL request.
  2. Select the Scripts tab.
  3. Select Packages at the lower right of the code editor, and select Open package library.
  4. Search for a package you'd like to delete.
  5. Select , then select Delete.
  1. To confirm your selection, select Yes, I understand.
Use external libraries in packages

You can use external library modules in packages. Learn about supported external library modules and how to use them.

You must use the require method to use the following external library modules in packages:

The following libraries are deprecated and no longer supported:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4