You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Revise connect-to-anonymous-apis documentation
Updated metadata and content for clarity and accuracy.
* Revise SharePoint API connection documentation
Updated the documentation to reflect new approaches for connecting to SharePoint APIs, including changes to the title, description, and date.
Copy file name to clipboardExpand all lines: docs/spfx/connect-to-anonymous-apis.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Connect to anonymous APIs
3
-
description: Different approaches of connecting to anonymous APIs from your SharePoint Framework solutions
4
-
ms.date: 12/04/2020
3
+
description: Different approaches for connecting to anonymous APIs from your SharePoint Framework solutions
4
+
ms.date: 03/11/2026
5
5
ms.localizationpriority: high
6
6
---
7
7
@@ -10,11 +10,11 @@ ms.localizationpriority: high
10
10
When building SharePoint Framework solutions, you might want to consume public APIs, such as stock or weather information. This article outlines how to connect to public APIs in SharePoint Framework solutions.
11
11
12
12
> [!NOTE]
13
-
> In this article, public and anonymous APIs are used interchangeably. This article is about connecting to APIs, that don't require authentication at all or are secured with a function/API key that can be passed via query string parameters. See other pages in this section of the documentation for more information about connecting to the [SharePoint APIs](connect-to-sharepoint.md) or [APIs secured with Azure AD](use-aadhttpclient.md).
13
+
> In this article, public and anonymous APIs are used interchangeably. This article is about connecting to APIs that don't require authentication at all or are secured with a function/API key that can be passed via query string parameters. See other pages in this section of the documentation for more information about connecting to the [SharePoint APIs](connect-to-sharepoint.md) or [APIs secured with Microsoft Entra ID](use-aadhttpclient.md).
14
14
15
15
## Connect to anonymous APIs using the HttpClient
16
16
17
-
The easiest way, to connect to anonymous APIs in your SharePoint Framework solutions, is by using the HttpClient provided as a part of the SharePoint Framework. For example, to get dummy information from the Typicode service, you would execute:
17
+
The easiest way to connect to anonymous APIs in your SharePoint Framework solutions is by using the HttpClient provided as a part of the SharePoint Framework. For example, to get dummy information from the Typicode service, you would execute:
18
18
19
19
```typescript
20
20
this.context.httpClient
@@ -27,7 +27,7 @@ this.context.httpClient
27
27
});
28
28
```
29
29
30
-
Similarly to the **SPHttpClient** you use for connecting to SharePoint APIs, the **HttpClient** offers you similar capabilities for running the most common web requests. If necessary, you can use its options, to configure requests.
30
+
Similarly to the **SPHttpClient** you use for connecting to SharePoint APIs, the **HttpClient** offers you similar capabilities for running the most common web requests. If necessary, you can use its options to configure requests.
31
31
32
32
For example, to specify request headers, you would use the following code:
33
33
@@ -57,4 +57,5 @@ While the **HttpClient** is similar to the **SPHttpClient**, it doesn't include
57
57
58
58
#### Part of the SharePoint Framework
59
59
60
-
The **HttpClient** is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page that is why using it doesn't cause additional performance overhead on runtime.
60
+
The **HttpClient** is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page, which is why using it doesn't cause additional performance overhead on runtime.
Copy file name to clipboardExpand all lines: docs/spfx/connect-to-sharepoint.md
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,17 @@
1
1
---
2
2
title: Connect to SharePoint APIs
3
-
description: Different approaches of connecting to SharePoint APIs from your SharePoint Framework solutions
4
-
ms.date: 02/24/2022
3
+
description: Different approaches for connecting to SharePoint APIs from your SharePoint Framework solutions
4
+
ms.date: 03/11/2026
5
5
ms.localizationpriority: high
6
6
---
7
7
8
8
# Connect to SharePoint APIs
9
9
10
-
In your SharePoint Framework solutions, you'll likely want to interact with data stored in SharePoint. SharePoint offers a rich set of APIs that can be consumed in various ways. This article outlines what options you have, how they work and what their advantages and disadvantages are.
10
+
In your SharePoint Framework solutions, you'll likely want to interact with data stored in SharePoint. SharePoint offers a rich set of APIs that can be consumed in various ways. This article outlines what options you have, how they work, and what their advantages and disadvantages are.
11
11
12
12
## Connect to SharePoint API using the SPHttpClient
13
13
14
-
SharePoint Framework offers the SPHttpClient that you can use to connect to SharePoint REST APIs. A ready-to-use instance of the SPHttpClient is available on the web part/extension context and you can use it to do all kinds of web request. Following code snippet shows how you would use the SPHttpClient to retrieve the title of the current site:
14
+
SharePoint Framework offers the SPHttpClient that you can use to connect to SharePoint REST APIs. A ready-to-use instance of the SPHttpClient is available on the web part/extension context and you can use it to do all kinds of web requests. The following code snippet shows how you would use the SPHttpClient to retrieve the title of the current site:
15
15
16
16
```typescript
17
17
this.context.spHttpClient
@@ -24,7 +24,7 @@ this.context.spHttpClient
24
24
});
25
25
```
26
26
27
-
The SPHttpClient offers basic functionality for performing the most common web requests. It allows you also, to configure your request by, for example, specifying request headers. For example, if you wanted to issue a web request without retrieving metadata, you'd use the following code:
27
+
The SPHttpClient offers basic functionality for performing the most common web requests. It also allows you to configure your request by, for example, specifying request headers. For example, if you wanted to issue a web request without retrieving metadata, you'd use the following code:
28
28
29
29
```typescript
30
30
this.context.spHttpClient
@@ -49,7 +49,7 @@ When using the SPHttpClient, there are a few things that you should take into ac
49
49
50
50
#### OData v4.0
51
51
52
-
By default, the SPHttpClient uses OData v4 specification, which requires using `odata.metadata` instead of just `odata` to control the response metadata. If you use the `odata` directive in OData v4 mode, you'll get an error, like: _The HTTP header ACCEPT is missing or its value is invalid._. It is possible to set the SPHttpClient to OData v3.0 mode, by setting the `odata-version` request header to empty value:
52
+
By default, the SPHttpClient uses OData v4 specification, which requires using `odata.metadata` instead of just `odata` to control the response metadata. If you use the `odata` directive in OData v4 mode, you'll get an error, like: _The HTTP header ACCEPT is missing or its value is invalid._. It is possible to set the SPHttpClient to OData v3.0 mode by setting the `odata-version` request header to empty value:
53
53
54
54
```typescript
55
55
this.context.spHttpClient
@@ -71,15 +71,15 @@ this.context.spHttpClient
71
71
72
72
#### Authentication cookies
73
73
74
-
In the SharePoint Framework, there are a number of classes for executing web requests. Two of them are the **SPHttpClient** and **HttpClient**. One of the differences between the SPHttpClient and HttpClient is that the SPHttpClient includes authentication cookies when issuing web requests. Because SharePoint APIs are not anonymous, you need to provide authentication information or you'll get an 401 Unauthorized response. Because the HttpClient doesn't include authentication cookies in its request, if you used it to call the SharePoint REST APIs, your requests would fail with a 401 Unauthorized response.
74
+
In the SharePoint Framework, there are a number of classes for executing web requests. Two of them are the **SPHttpClient** and **HttpClient**. One of the differences between the SPHttpClient and HttpClient is that the SPHttpClient includes authentication cookies when issuing web requests. Because SharePoint APIs are not anonymous, you need to provide authentication information or you'll get a 401 Unauthorized response. Because the HttpClient doesn't include authentication cookies in its requests, if you used it to call the SharePoint REST APIs, your requests would fail with a 401 Unauthorized response.
75
75
76
76
#### Part of the SharePoint Framework
77
77
78
-
The SPHttpClient is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page that is why using it doesn't cause additional performance overhead on runtime.
78
+
The SPHttpClient is part of the SharePoint Framework and you don't need any additional dependencies to start using it. It is already available on the page, which is why using it doesn't cause additional performance overhead at runtime.
79
79
80
80
#### Raw REST queries are error-prone
81
81
82
-
The SPHttpClient offers basic support for communicating with the SharePoint REST API. If your applications require more complex GET requests, POST requests or uses more advanced capabilities such as batching, you'll quickly notice that using the SPHttpClient is cumbersome and error-prone. In such cases, you should consider using an alternative such as the PnPjs library that offers you a fluent API that can be verified for correctness by TypeScript.
82
+
The SPHttpClient offers basic support for communicating with the SharePoint REST API. If your applications require more complex GET requests, POST requests or use more advanced capabilities such as batching, you'll quickly notice that using the SPHttpClient is cumbersome and error-prone. In such cases, you should consider using an alternative such as the PnPjs library that offers you a fluent API that can be verified for correctness by TypeScript.
Notice, how less verbose the code is comparing to the SharePoint Framework SPHttpClient and how all elements of the requests, except for the names of the properties to retrieve, are strongly typed lowering the risk of runtime errors.
97
+
Notice how much less verbose the code is compared to the SharePoint Framework SPHttpClient and how all elements of the requests, except for the names of the properties to retrieve, are strongly typed lowering the risk of runtime errors.
98
98
99
99
For more information about how to setup and use PnPjs in the SharePoint Framework, see the PnPjs documentation at [https://pnp.github.io/pnpjs/](https://pnp.github.io/pnpjs/).
100
100
101
101
### Considerations for using PnPjs
102
102
103
103
When deciding whether you should use PnPjs or not, following are a few considerations that you should take into account.
104
104
105
-
#### Raw REST queries are errorprone
105
+
#### Raw REST queries are error-prone
106
106
107
-
Issuing raw REST requests using the SPHttpClient is error-prone. Especially, when your application will need to execute POST queries or you'll want to use some of the more advanced capabilities such as request batching, composing the correct requests and parsing the responses will be cumbersome. Not to mention, the only way to verify if the requests are correct is by running the code in the browser. With PnPjs, you can communicate with SharePoint APIs in a type-safe way and easily use the advanced capabilities of SharePoint API what allows you to focus on building your application instead of testing its requests.
107
+
Issuing raw REST requests using the SPHttpClient is error-prone. Especially when your application needs to execute POST queries or you'll want to use some of the more advanced capabilities such as request batching, composing the correct requests and parsing the responses will be cumbersome. Not to mention, the only way to verify if the requests are correct is by running the code in the browser. With PnPjs, you can communicate with SharePoint APIs in a type-safe way and easily use the advanced capabilities of the SharePoint API, which allows you to focus on building your application instead of testing its requests.
108
108
109
109
#### Open-source project
110
110
111
111
PnPjs is an open-source project managed by the SharePoint community. There's no SLA for using PnPjs in your solutions and Microsoft support won't assist you with any possible issues when caused by PnPjs. That said, PnPjs is actively developed and the community quickly responds to all submitted issues and questions.
112
112
113
113
#### Additional dependency
114
114
115
-
PnPjs is an additional dependency that you need to add to your project and manage over time. You need to keep track of its updates and upgrade your project when necessary. The community behind PnPjs regularly communicates the state of the current work, upcoming releases, and the impact, if any, of upgrading to the newer version.
115
+
PnPjs is an additional dependency that you need to add to your project and manage over time. You need to keep track of its updates and upgrade your project when necessary. The community behind PnPjs regularly communicates the state of the current work, upcoming releases, and the impact, if any, of upgrading to a newer version.
116
116
117
117
#### Additional payload
118
118
119
-
PnPjs offers a rich set of capabilities for communicating with SharePoint APIs. The library supports selective imports so with carful curation the overall impact to your bundle size can be mitigated. For more information please see the documentation at [https://pnp.github.io/pnpjs/](https://pnp.github.io/pnpjs/).
119
+
PnPjs offers a rich set of capabilities for communicating with SharePoint APIs. The library supports selective imports so with careful curation the overall impact to your bundle size can be mitigated. For more information, please see the documentation at [https://pnp.github.io/pnpjs/](https://pnp.github.io/pnpjs/).
0 commit comments