Registries

Cargo installs crates and fetches dependencies from a "registry". The defaultregistry is crates.io. A registry contains an "index" which contains asearchable list of available crates. A registry may also provide a web API tosupport publishing new crates directly from Cargo.

Note: If you are interested in mirroring or vendoring an existing registry,take a look at Source Replacement.

Using an Alternate Registry

To use a registry other than crates.io, the name and index URL of theregistry must be added to a .cargo/config file. The registriestable has a key for each registry, for example:

  1. [registries]
  2. my-registry = { index = "https://my-intranet:8080/git/index" }

The index key should be a URL to a git repository with the registry's index.A crate can then depend on a crate from another registry by specifying theregistry key and a value of the registry's name in that dependency's entryin Cargo.toml:

  1. # Sample Cargo.toml
  2. [package]
  3. name = "my-project"
  4. version = "0.1.0"
  5. [dependencies]
  6. other-crate = { version = "1.0", registry = "my-registry" }

As with most config values, the index may be specified with an environmentvariable instead of a config file. For example, setting the followingenvironment variable will accomplish the same thing as defining a config file:

  1. CARGO_REGISTRIES_MY_REGISTRY_INDEX=https://my-intranet:8080/git/index

Note: crates.io does not accept packages that depend on crates from otherregistries.

Publishing to an Alternate Registry

If the registry supports web API access, then packages can be publisheddirectly to the registry from Cargo. Several of Cargo's commands such ascargo publish take a —registry command-line flag to indicate whichregistry to use. For example, to publish the package in the current directory:

  • cargo login —registry=my-registry

This only needs to be done once. You must enter the secret API tokenretrieved from the registry's website. Alternatively the token may bepassed directly to the publish command with the —token command-lineflag or an environment variable with the name of the registry such asCARGO_REGISTRIES_MY_REGISTRY_TOKEN.

  • cargo publish —registry=my-registry

Instead of always passing the —registry command-line option, the defaultregistry may be set in .cargo/config with the registry.defaultkey.

Setting the package.publish key in the Cargo.toml manifest restricts whichregistries the package is allowed to be published to. This is useful toprevent accidentally publishing a closed-source package to crates.io. Thevalue may be a list of registry names, for example:

  1. [package]
  2. # ...
  3. publish = ["my-registry"]

The publish value may also be false to restrict all publishing, which isthe same as an empty list.

The authentication information saved by cargo login is stored in thecredentials file in the Cargo home directory (default $HOME/.cargo). Ithas a separate table for each registry, for example:

  1. [registries.my-registry]
  2. token = "854DvwSlUwEHtIo3kWy6x7UCPKHfzCmy"

Running a Registry

A minimal registry can be implemented by having a git repository that containsan index, and a server that contains the compressed .crate files created bycargo package. Users won't be able to use Cargo to publish to it, but thismay be sufficient for closed environments.

A full-featured registry that supports publishing will additionally need tohave a web API service that conforms to the API used by Cargo. The web API isdocumented below.

At this time, there is no widely used software for running a custom registry.There is interest in documenting projects that implement registry support, orexisting package caches that add support for Cargo.

Index Format

The following defines the format of the index. New features are occasionallyadded, which are only understood starting with the version of Cargo thatintroduced them. Older versions of Cargo may not be able to use packages thatmake use of new features. However, the format for older packages should notchange, so older versions of Cargo should be able to use them.

The index is stored in a git repository so that Cargo can efficiently fetchincremental updates to the index. In the root of the repository is a filenamed config.json which contains JSON information used by Cargo foraccessing the registry. This is an example of what the crates.io config filelooks like:

  1. {
  2. "dl": "https://crates.io/api/v1/crates",
  3. "api": "https://crates.io"
  4. }

The keys are:

  • dl: This is the URL for downloading crates listed in the index. The valuemay have the markers {crate} and {version} which are replaced with thename and version of the crate to download. If the markers are not present,then the value /{crate}/{version}/download is appended to the end.
  • api: This is the base URL for the web API. This key is optional, but if itis not specified, commands such as cargo publish will not work. The webAPI is described below.The download endpoint should send the .crate file for the requested package.Cargo supports https, http, and file URLs, HTTP redirects, HTTP1 and HTTP2.The exact specifics of TLS support depend on the platform that Cargo isrunning on, the version of Cargo, and how it was compiled.

The rest of the index repository contains one file for each package, where thefilename is the name of the package in lowercase. Each version of the packagehas a separate line in the file. The files are organized in a tier ofdirectories:

  • Packages with 1 character names are placed in a directory named 1.
  • Packages with 2 character names are placed in a directory named 2.
  • Packages with 3 character names are placed in the directory3/{first-character} where {first-character} is the first character ofthe package name.
  • All other packages are stored in directories named{first-two}/{second-two} where the top directory is the first twocharacters of the package name, and the next subdirectory is the third andfourth characters of the package name. For example, cargo would be storedin a file named ca/rg/cargo.

Note: Although the index filenames are in lowercase, the fields that containpackage names in Cargo.toml and the index JSON data are case-sensitive andmay contain upper and lower case characters.

Registries may want to consider enforcing limitations on package names addedto their index. Cargo itself allows names with any alphanumeric, -, or _character. For example, crates.io imposes relatively strict limitations,such as requiring it to be a valid Rust identifier, only allowing ASCIIcharacters, under a specific length, and rejects reserved names such asWindows special filenames like "nul".

Each line in a package file contains a JSON object that describes a publishedversion of the package. The following is a pretty-printed example with commentsexplaining the format of the entry.

  1. {
  2. // The name of the package.
  3. // This must only contain alphanumeric, `-`, or `_` characters.
  4. "name": "foo",
  5. // The version of the package this row is describing.
  6. // This must be a valid version number according to the Semantic
  7. // Versioning 2.0.0 spec at https://semver.org/.
  8. "vers": "0.1.0",
  9. // Array of direct dependencies of the package.
  10. "deps": [
  11. {
  12. // Name of the dependency.
  13. // If the dependency is renamed from the original package name,
  14. // this is the new name. The original package name is stored in
  15. // the `package` field.
  16. "name": "rand",
  17. // The semver requirement for this dependency.
  18. // This must be a valid version requirement defined at
  19. // https://github.com/steveklabnik/semver#requirements.
  20. "req": "^0.6",
  21. // Array of features (as strings) enabled for this dependency.
  22. "features": ["i128_support"],
  23. // Boolean of whether or not this is an optional dependency.
  24. "optional": false,
  25. // Boolean of whether or not default features are enabled.
  26. "default_features": true,
  27. // The target platform for the dependency.
  28. // null if not a target dependency.
  29. // Otherwise, a string such as "cfg(windows)".
  30. "target": null,
  31. // The dependency kind.
  32. // "dev", "build", or "normal".
  33. // Note: this is a required field, but a small number of entries
  34. // exist in the crates.io index with either a missing or null
  35. // `kind` field due to implementation bugs.
  36. "kind": "normal",
  37. // The URL of the index of the registry where this dependency is
  38. // from as a string. If not specified or null, it is assumed the
  39. // dependency is in the current registry.
  40. "registry": null,
  41. // If the dependency is renamed, this is a string of the actual
  42. // package name. If not specified or null, this dependency is not
  43. // renamed.
  44. "package": null,
  45. }
  46. ],
  47. // A SHA256 checksum of the `.crate` file.
  48. "cksum": "d867001db0e2b6e0496f9fac96930e2d42233ecd3ca0413e0753d4c7695d289c",
  49. // Set of features defined for the package.
  50. // Each feature maps to an array of features or dependencies it enables.
  51. "features": {
  52. "extras": ["rand/simd_support"]
  53. },
  54. // Boolean of whether or not this version has been yanked.
  55. "yanked": false,
  56. // The `links` string value from the package's manifest, or null if not
  57. // specified. This field is optional and defaults to null.
  58. "links": null
  59. }

The JSON objects should not be modified after they are added except for theyanked field whose value may change at any time.

Web API

A registry may host a web API at the location defined in config.json tosupport any of the actions listed below.

Cargo includes the Authorization header for requests that requireauthentication. The header value is the API token. The server should respondwith a 403 response code if the token is not valid. Users are expected tovisit the registry's website to obtain a token, and Cargo can store the tokenusing the cargo login command, or by passing the token on thecommand-line.

Responses use a 200 response code for both success and errors. Cargo looks atthe JSON response to determine if there was success or failure. Failureresponses have a JSON object with the following structure:

  1. {
  2. // Array of errors to display to the user.
  3. "errors": [
  4. {
  5. // The error message as a string.
  6. "detail": "error message text"
  7. }
  8. ]
  9. }

Servers may also respond with a 404 response code to indicate the requestedresource is not found (for example, an unknown crate name). However, using a200 response with an errors object allows a registry to provide a moredetailed error message if desired.

For backwards compatibility, servers should ignore any unexpected queryparameters or JSON fields. If a JSON field is missing, it should be assumed tobe null. The endpoints are versioned with the v1 component of the path, andCargo is responsible for handling backwards compatibility fallbacks should anybe required in the future.

Cargo sets the following headers for all requests:

  • Content-Type: application/json
  • Accept: application/json
  • User-Agent: The Cargo version such as cargo 1.32.0 (8610973aa 2019-01-02). This may be modified by the user in a configuration value.Added in 1.29.

Publish

  • Endpoint: /api/v1/crates/new
  • Method: PUT
  • Authorization: IncludedThe publish endpoint is used to publish a new version of a crate. The servershould validate the crate, make it available for download, and add it to theindex.

The body of the data sent by Cargo is:

  • 32-bit unsigned little-endian integer of the length of JSON data.
  • Metadata of the package as a JSON object.
  • 32-bit unsigned little-endian integer of the length of the .crate file.
  • The .crate file.The following is a commented example of the JSON object. Some notes of somerestrictions imposed by crates.io are included only to illustrate somesuggestions on types of validation that may be done, and should not beconsidered as an exhaustive list of restrictions crates.io imposes.
  1. {
  2. // The name of the package.
  3. "name": "foo",
  4. // The version of the package being published.
  5. "vers": "0.1.0",
  6. // Array of direct dependencies of the package.
  7. "deps": [
  8. {
  9. // Name of the dependency.
  10. // If the dependency is renamed from the original package name,
  11. // this is the original name. The new package name is stored in
  12. // the `explicit_name_in_toml` field.
  13. "name": "rand",
  14. // The semver requirement for this dependency.
  15. "version_req": "^0.6",
  16. // Array of features (as strings) enabled for this dependency.
  17. "features": ["i128_support"],
  18. // Boolean of whether or not this is an optional dependency.
  19. "optional": false,
  20. // Boolean of whether or not default features are enabled.
  21. "default_features": true,
  22. // The target platform for the dependency.
  23. // null if not a target dependency.
  24. // Otherwise, a string such as "cfg(windows)".
  25. "target": null,
  26. // The dependency kind.
  27. // "dev", "build", or "normal".
  28. "kind": "normal",
  29. // The URL of the index of the registry where this dependency is
  30. // from as a string. If not specified or null, it is assumed the
  31. // dependency is in the current registry.
  32. "registry": null,
  33. // If the dependency is renamed, this is a string of the new
  34. // package name. If not specified or null, this dependency is not
  35. // renamed.
  36. "explicit_name_in_toml": null,
  37. }
  38. ],
  39. // Set of features defined for the package.
  40. // Each feature maps to an array of features or dependencies it enables.
  41. // Cargo does not impose limitations on feature names, but crates.io
  42. // requires alphanumeric ASCII, `_` or `-` characters.
  43. "features": {
  44. "extras": ["rand/simd_support"]
  45. },
  46. // List of strings of the authors.
  47. // May be empty. crates.io requires at least one entry.
  48. "authors": ["Alice <a@example.com>"],
  49. // Description field from the manifest.
  50. // May be null. crates.io requires at least some content.
  51. "description": null,
  52. // String of the URL to the website for this package's documentation.
  53. // May be null.
  54. "documentation": null,
  55. // String of the URL to the website for this package's home page.
  56. // May be null.
  57. "homepage": null,
  58. // String of the content of the README file.
  59. // May be null.
  60. "readme": null,
  61. // String of a relative path to a README file in the crate.
  62. // May be null.
  63. "readme_file": null,
  64. // Array of strings of keywords for the package.
  65. "keywords": [],
  66. // Array of strings of categories for the package.
  67. "categories": [],
  68. // String of the license for the package.
  69. // May be null. crates.io requires either `license` or `license_file` to be set.
  70. "license": null,
  71. // String of a relative path to a license file in the crate.
  72. // May be null.
  73. "license_file": null,
  74. // String of the URL to the website for the source repository of this package.
  75. // May be null.
  76. "repository": null,
  77. // Optional object of "status" badges. Each value is an object of
  78. // arbitrary string to string mappings.
  79. // crates.io has special interpretation of the format of the badges.
  80. "badges": {
  81. "travis-ci": {
  82. "branch": "master",
  83. "repository": "rust-lang/cargo"
  84. }
  85. },
  86. // The `links` string value from the package's manifest, or null if not
  87. // specified. This field is optional and defaults to null.
  88. "links": null,
  89. }

A successful response includes the JSON object:

  1. {
  2. // Optional object of warnings to display to the user.
  3. "warnings": {
  4. // Array of strings of categories that are invalid and ignored.
  5. "invalid_categories": [],
  6. // Array of strings of badge names that are invalid and ignored.
  7. "invalid_badges": [],
  8. // Array of strings of arbitrary warnings to display to the user.
  9. "other": []
  10. }
  11. }

Yank

  • Endpoint: /api/v1/crates/{crate_name}/{version}/yank
  • Method: DELETE
  • Authorization: IncludedThe yank endpoint will set the yank field of the given version of a crate totrue in the index.

A successful response includes the JSON object:

  1. {
  2. // Indicates the delete succeeded, always true.
  3. "ok": true,
  4. }

Unyank

  • Endpoint: /api/v1/crates/{crate_name}/{version}/unyank
  • Method: PUT
  • Authorization: IncludedThe unyank endpoint will set the yank field of the given version of a crateto false in the index.

A successful response includes the JSON object:

  1. {
  2. // Indicates the delete succeeded, always true.
  3. "ok": true,
  4. }

Owners

Cargo does not have an inherent notion of users and owners, but it doesprovide the owner command to assist managing who has authorization tocontrol a crate. It is up to the registry to decide exactly how users andowners are handled. See the publishing documentation for a description ofhow crates.io handles owners via GitHub users and teams.

Owners: List
  • Endpoint: /api/v1/crates/{crate_name}/owners
  • Method: GET
  • Authorization: IncludedThe owners endpoint returns a list of owners of the crate.

A successful response includes the JSON object:

  1. {
  2. // Array of owners of the crate.
  3. "users": [
  4. {
  5. // Unique unsigned 32-bit integer of the owner.
  6. "id": 70,
  7. // The unique username of the owner.
  8. "login": "github:rust-lang:core",
  9. // Name of the owner.
  10. // This is optional and may be null.
  11. "name": "Core",
  12. }
  13. ]
  14. }
Owners: Add
  • Endpoint: /api/v1/crates/{crate_name}/owners
  • Method: PUT
  • Authorization: IncludedA PUT request will send a request to the registry to add a new owner to acrate. It is up to the registry how to handle the request. For example,crates.io sends an invite to the user that they must accept before beingadded.

The request should include the following JSON object:

  1. {
  2. // Array of `login` strings of owners to add.
  3. "users": ["login_name"]
  4. }

A successful response includes the JSON object:

  1. {
  2. // Indicates the add succeeded, always true.
  3. "ok": true,
  4. // A string to be displayed to the user.
  5. "msg": "user ehuss has been invited to be an owner of crate cargo"
  6. }
Owners: Remove
  • Endpoint: /api/v1/crates/{crate_name}/owners
  • Method: DELETE
  • Authorization: IncludedA DELETE request will remove an owner from a crate. The request should includethe following JSON object:
  1. {
  2. // Array of `login` strings of owners to remove.
  3. "users": ["login_name"]
  4. }

A successful response includes the JSON object:

  1. {
  2. // Indicates the remove succeeded, always true.
  3. "ok": true
  4. }
  • Endpoint: /api/v1/crates
  • Method: GET
  • Query Parameters:
    • q: The search query string.
    • per_page: Number of results, default 10, max 100.The search request will perform a search for crates, using criteria defined onthe server.

A successful response includes the JSON object:

  1. {
  2. // Array of results.
  3. "crates": [
  4. {
  5. // Name of the crate.
  6. "name": "rand",
  7. // The highest version available.
  8. "max_version": "0.6.1",
  9. // Textual description of the crate.
  10. "description": "Random number generators and other randomness functionality.\n",
  11. }
  12. ],
  13. "meta": {
  14. // Total number of results available on the server.
  15. "total": 119
  16. }
  17. }

Login

  • Endpoint: /meThe "login" endpoint is not an actual API request. It exists solely for thecargo login command to display a URL to instruct a user to visit in a webbrowser to log in and retrieve an API token.